The fact that the dispatch timer, updates on the UI thread, is convenient – with one problem… IT CAUSES THE UI TO FREEZE AT TIMES!
As such, i would like to use the Timer in the System.Timers namespace, which will achieve the same thing except, that the UI will be more responsive as it updates from a different thread.
System.Timers.Timer requires a SynchronisationObject, as such i would like to implement ISynchronizeInvoke on my WPF window.
Can someone please help me understand how to go about implementing this and also verify if my thinking here is correct or not.
Any help would be appreciated.
Well, answering the question directly: Can you not just write an implementation of
ISynchronizeInvokewhich accepts aDispatcherin its constructor and just forwards all the calls? TheDispatcherAPI is very, very similar to theISynchronizeInvokeone.However, I don’t think this is really the right answer – because the point of giving a
TimeranISynchronizeInvokeis that it will use it to marshal to the UI thread (or whatever). That’s going to leave you in exactly the same situation as before. Presumably you have work which you want to execute not on the UI thread… so you don’t want to pass in anISynchronizeInvokeafter all. Just use a normal timer (or .NET 4 Parallel Extensions tasks etc) and useDispatcher.InvokeorDispatcher.BeginInvokejust for the times where you need to update the UI (which has to be done on the UI thread, of course).