How to acces datagrid from other thread

  • Thread starter Thread starter PiotrKolodziej
  • Start date Start date
P

PiotrKolodziej

Hi.
Iam trying to make it work:

My application receives data from RS port by the DataReceived event. Then
this event is accesing dataGridview, but when i have some errors connected
with accesing this datagrid. Sometimes it works properly, sometimes not.

For any help thanks
Here is sample code:

void rs_DataReceived(object sender, SerialDataReceivedEventArgs e)
{
try
{
string t = rs.ReadLine(); t = t.Remove(t.Length - 1);
string temp = configFile.getParameter("b" + t);

if (temp == null)
{
if (MessageBox.Show("Button is not recognized. Would you
like to add function to this button?", "New Button detected",
MessageBoxButtons.YesNo,
MessageBoxIcon.Information) == DialogResult.Yes)
{
Form2 form2 = new Form2();
form2.Button = "b" + t;

if (form2.ShowDialog() != DialogResult.Cancel)
{
//System.Threading.Thread.Sleep(500);
Fill_DataGrid();
}
}
.........
}
 
U¿ytkownik "PiotrKolodziej said:
Hi.
Iam trying to make it work:

My application receives data from RS port by the DataReceived event. Then
this event is accesing dataGridview, but when i have some errors connected
with accesing this datagrid. Sometimes it works properly, sometimes not.

Use BeginInvoke or Invoke to fill DataGrid on GUI thread, for example:

delegate void myDelegate();
private void rs_DataReceived(...)
{
...
// fill data grid on form2
form2.BeginInvoke(new myDelegate(Fill_DataGrid),
new object[]{data});
}

private void Fill_DataGrid()
{
...
}

Grzegorz

ps. why not alt.pl.comp.lang.csharp?
 
Back
Top