Groups | Blog | Home
all groups > dotnet general > may 2006 >

dotnet general : How to acces datagrid from other thread


PiotrKolodziej
5/20/2006 9:51:39 PM
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();
}
}
.........
}

Grzegorz Danowski
5/21/2006 3:45:40 PM
U¿ytkownik "PiotrKolodziej" <piotr.kolodziej@gmail.com> napisa³ w wiadomo¶ci
news:ae438$446f7340$57cf8020$1058@news.chello.pl...
[quoted text, click to view]

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?
AddThis Social Bookmark Button