I have built a component library that includes an executive class which does work on any number of threads and fires events from these threads. This is all good. Now I want to use my executive in a desktop app. (Winforms in the first instance, WPF later on) so I want to marshall all event calls back onto the UI thread. I know of a 3 ways to do this;
- Check IsInvokedRequired/call Invoke in the handlers; This is lame IMO.
- Create a decorator for the executive that uses the event base async model; Gives me the desired result, not very exciting though.
- Use the WPF dispatcher; feels wrong to use a WPF class in Winforms app. or even more wrong to use it in the component lib.
I have spent the last hour or so reading up on Rx and I’m thinking the ideal solution might be to bake Rx into the executive and have the executive take (optionaly) a scheduler. This way the client of the executive can determine the behaviour with regard to the which thread/s the events are raised on and I get all of the other Rx goodness. Or perhaps create an RxExecutive that takes a Scheduler and encapsulates my existing executive to provide an Rx API.
Am I thinking along the right lines or have I missed the point?
Rx does provide a very nice way to access the UI thread in a Windows Forms app. You can use the full Rx library of observables, but if you just need an easy way to run things on the UI thread then using the
ControlScheduleris a snap.Assuming you have a form called
form1just do this:Easy.
You don’t have to use a reference to the form – you could use any control.