"Ole" <ol*@blabla.comwrote in message
news:er**************@TK2MSFTNGP02.phx.gbl...
I have an instance of a class in which a thread is running. When a special
condition in that instance is met it raises an event to my main class
(UI), but I can't directly interact with the user interface controls in my
eventhandler without some sort of invoke. How should that be done - is it
possible to locate the invoke in the instance so that the eventhandler
actually belongs to the UI?
The Invoke method should be invoked on a Control belonging to th UI. If
you pass to the instance of your class one such control (for instance the
"this" from the main form in your application), then it can use such control
to call Invoke and marshall execution into the main thread before raising
the event. Something similar to the following:
class MyClass
{
private Control ctl;
public Myclass(Control ctl)
{
this.ctl=ctl;
}
public event EventHandler theEvent;
private void EventRaiser()
{
if (theEvent!=null) theEvent(this, EventArgs.Empty);
}
void CodeThatRaisesEvent()
{
ctl.Invoke(new MethodInvoker(EventRaiser));
}
}