Basic forms\threading question...

  • Thread starter Thread starter Tom Jones
  • Start date Start date
T

Tom Jones

I know it is a no-no to [directly] manipulate a UI control from a thread
other than the thread used to create that control.

I have a situation where my my form loads an assembly, then registeres a
callback with a component from that assembly. The component has its own
internal thread-pool. I am not sure what to do when I am called back (at
which point I am running on a thread that cannot update my UI).

Any suggestions/pointers/etc. would be appreciated.

Thanks,
Tom
 
You can use InvokeRequired (inherited from Control) to determine if your method is being called from a non-UI thread and use Invoke or BeginInvoke (both also inherited from Control) if it is.

public void MyCallBackMethod()
{
if(this.InvokeRequired)
{
this.Invoke(new delegateName(methodName), new object[] { arg1, arg2 } );
}
else
{
// proceed normally. No Invoke required
}
}
 
What exactly do you want to do to the UI? If it is a simple redraw then just
'Refresh' the components in question. Presumably your thread is updating
state information that the UI will use when it needs to redraw, so
refreshing the component should (hate to use that word, sorry) cause the
component to pick up this state information and redraw itself as you need.
Don't forget to code up the appropriate paint event handler though.

--
 
Back
Top