In my C++/Qt applications, whenever I want to raise a signal/event, I would simply do:
emit Event();
This would call all handlers for Event()
on the same thread as the objects that subscribed to this event.
C# doesn't seem to offer anything like that, so how do I do this?
I have a class, let's call it EventRaiser
, which has a SomethingHappened
event and a method that raises the event when needed:
class EventRaiser
{
public event EventHandler SomethingHappened;
void RaiseEvent()
{
var anyoneWhosInterested = SomethingHappened;
if (anyoneWhosInterested != null)
{
try { anyoneWhosInterested(this, new EventArgs()); }
catch { /* we don't care */ }
}
}
}
My problem is that RaiseEvent()
will call the handlers on the thread RaiseEvent()
got called. But I need to call the handlers on the threads that subscribed to the event.
How do I do this?
I'm on .NET 2.0 and Visual Studio 2012.