c0D3l0g1c
c0D3l0g1c

Reputation: 3164

WPF Window Implement ISynchronizeInvoke for use with System.Timers.Timer

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.

Upvotes: 1

Views: 2303

Answers (1)

Jon Skeet
Jon Skeet

Reputation: 1500505

Well, answering the question directly: Can you not just write an implementation of ISynchronizeInvoke which accepts a Dispatcher in its constructor and just forwards all the calls? The Dispatcher API is very, very similar to the ISynchronizeInvoke one.

However, I don't think this is really the right answer - because the point of giving a Timer an ISynchronizeInvoke is 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 an ISynchronizeInvoke after all. Just use a normal timer (or .NET 4 Parallel Extensions tasks etc) and use Dispatcher.Invoke or Dispatcher.BeginInvoke just for the times where you need to update the UI (which has to be done on the UI thread, of course).

Upvotes: 3

Related Questions