Showing posts with label Multiple Threading. Show all posts
Showing posts with label Multiple Threading. Show all posts

Thursday, January 29, 2015

Winforms - Update Control from Another Thread

Assume you're using a grid view to display status for some network devices, and you have a background thread which receives status update and then update the grid view.

You could do the update safely with the following:
public delegate void UpdateStatusDelegate(StatusData data);

public void UpdateStatus(StatusData data)
{
  if (!this.radGridView1.InvokeRequired)
  {
    UpdateStatus0(data);
  }
  else
  {
    UpdateStatusDelegate del = new UpdateStatusDelegate(UpdateStatusRowStatus);
    this.radGridView1.Invoke(del, new object[] { data });
  }
}

private void UpdateStatus0(StatusData data)
{//do actual update thing
} 

Here 'radGridView1' is the grid view control (which is a control in Telerik's Winform library).

You should NOT simply call UpdateStatus0(). Doing that will give you exception at runtime.



Create a Successful Online Store at Bigcommerce! Try it Free Now!