I have a WPF application with lots of user controls. One of these controls also uses a 3rd party DLL that watches an external system and produces events. I subscribe to those events and handle them with something like this:
public class ControlClassD
{
private 3rdPartyEventSource _3rdPartyEventSource = new 3rdPartyEventSource();
public ControlClassD()
{
_3rdPartyEventSource.NewEvent += _3rdPartyEventSource_NewEvent;
_3rdPartyEventSource.StartMakingEventsWhenSomethingHappens();
}
private void _3rdPartyEventSource_NewEvent(object o)
{
InstanceOfControlClassA.doSomethingWith(o);
InstanceOfControlClassB.doSomethingWith(o);
InstanceOfControlClassC.doSomethingWith(o);
}
}
All of the InstanceOfControlClassx were instantiated by whatever thread runs the _Loaded event handler in the MainWindow class at startup.
The thread executing the handler is one created by the 3rdPartyEventSource and has no access to all these things (as demonstrated by error messages of that nature)
What I would like to do is let the thread delivered by the 3rdPartyEventSource go back and have HandleNewEvent executed by the thread that created all of those instances (CreatorThread). Like:
private void _3rdPartyEventSource_NewEvent(object o)
{
SomehowInvokeCreatorThread(new Action(() => HandleNewEvent(o)));
}
private void HandleNewEvent(object o)
{
InstanceOfControlClassA.doSomethingWith(o);
InstanceOfControlClassB.doSomethingWith(o);
InstanceOfControlClassC.doSomethingWith(o); //which may access this
}
How can I do that?