If a non-gui object constructor wires a local event handling method to the event field of an object on a different thread, is it possible for said event handling method to be called before the constructor is finished?
Example: (semantic pseudocode only)
public static B b = new B();
class A
{
public A()
{
b.evt += EventHandler();
Thread.Sleep(5000);
}
protected void EventHandler()
{
// Some stuff
}
}
class B
{
public event evt;
public void ThreadedLoop()
{
while (true)
{
RaiseEvt();
}
}
}
Yes, because the event handling method is called in the context of the other thread.
In your example, the event handler is a method of class A, but when it is invoked, it is invoked in the context of the thread represented by class B. When the thread executing class A’s constructor sleeps, the thread represented by class B will go to work, raising the event and calling the event handler in the process.