Reputation: 2790
I have a System.Timers.Timer
object, and within its Elapsed method I want to call myObject.TriggerEvent(...)
, but I want that invocation to happen on the thread myObject
was created on. Is there an easy way to do this? And is this a bad idea for any reason? myObject
is basically an event manager service, so I want it to trigger events on the thread it was created on. The reason I'm using the timer is because I have another that is polling a service for updates, which it notifies myObject
of via the TriggerEvent
method.
Upvotes: 0
Views: 163
Reputation: 7449
I would recommend using a Dispatcher. It was created in the context of WPF, but there is no reason it can't be used elsewhere. I've used it in a couple of times, but I always had a message pump (Win32), but I believe that it can be used in service contexts as well (IIS, etc) - however I have not written such code, so YMMV. This is a good article from a WPF angle.
Erick
Upvotes: 0
Reputation: 37770
You can make your own SynchronizationContext implementation and provide its instance to the caller of myObject.TriggerEvent
. If you're using myObject
in WPF, SL or WinForms application, you can use their existing implementations of SynchronizationContext class and create myObject
on the GUI thread.
Upvotes: 1