August 19, 2006

BeginInvoke On A Gui Object

Good article on BeginInvoke and Invoke differences

On the GUI class (form or control derived object):

  private delegate void DataChanged(Object Sender, BindingEventArgs Args);

  private void HandleDataChanged(Object
Sender, BindingEventArgs Args)
  {
    // If this delegate is invoked on another thread
    if (InvokeRequired)
    {
      // Asynchronously call THIS method later
      // but this time on our own GUI thread
      // BeginInvoke translates to a PostMessage call on
      // our window.
      BeginInvoke(new DataChangedDelegate(HandleDataChanged),
 new object[] { Sender, Args });
    }
    else // when the method is invoked on the GUI thread
    { // Update our forms/controls as required
      UpdateGui(Args);
    }
  }

No comments: