views:

111

answers:

1

Hi All

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.

+1  A: 

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).

Jon Skeet
Hi JonThanks for the response. I currently do not have any UI code running in the timer event, but the application is growing, i suspect UI interaction will arise soon. When this happens, i would like the code to continue running as is.By implementing ISynchroniseInvoke, does that mean i would not have to call the Dispatcher.Invoke and Dispatcher.BeginInvoke on the UI element i want to manipulate? If so, this is very desirable - so i would really like to implement ISynchronizeInvoke, but i don't know how to.
c0D3l0g1
@c0D310g1: You've missed my point. If you implement `ISynchronizeInvoke`, you're effectively going back to the `DispatcherTimer` solution... all your code will be running on the UI thread, so it'll make the UI freeze again. If you want to run timed tasks on the UI thread, that's exactly what `DispatcherTimer` is there for. But you *don't* want to do that for long-running tasks, precisely because it locks up the UI.
Jon Skeet
That makes sense, thanks!
c0D3l0g1