3

In my WPF application i use 3 different DispatcherTimers.

  • One is for displaying the current time
  • One is are for running a DB query every 5 sec
  • The third one refreshes a value for a custom button every 1 sec

When my program is running there are a lot of delays / freezes. For example the time starts ticking correctly but on a sudden the value freezes up and after the freeze the time gets incremented by +3 seconds for example.

I am getting this behavior over my entire application. What is the proper way to solve this problem with several timers?

EDIT:

I am having problems to replace a DispatcherTimer with a Timer from System.Timers namespace.

//new code with timer
timerW = new Timer();
        timerW.Elapsed += new ElapsedEventHandler(timerWegingen_Tick);
        timerW.Interval = 5000; 
        timerW.Start();

        //OLD Working Code
        timerW = new DispatcherTimer();
        timerW.Tick += new EventHandler(timerWegingen_Tick);
        timerW.Interval = new TimeSpan(0, 0,5);
        timerW.Start();

Error : "The calling thread must be STA, because many UI components require this."

Best regards.

Jackz
  • 151
  • 4
  • 10

3 Answers3

3

the DispatcherTimer is executed on the UI thread. So if the UI thread is busy for more than the interval, it will be executed when the UI thread is free to do so. If you need more precise scheduling, you should go for a time than runs in the background (System.Threading.Timer, System.Timers.Timer). But don't forget about marshalling then.

hth, Martin

Martin Moser
  • 6,219
  • 1
  • 27
  • 41
  • What do you mean with marshaling exactly? Never really used threads in a real program before. So i can perfectly replace my Dispatcher timer functionality with a other type of timer? – Jackz Apr 16 '12 at 11:41
  • 1
    the events will be fired on a different thread, so you need to execute the UI then on the UI thread. And thats marshalling – Martin Moser Apr 16 '12 at 14:19
0

I guess this is because they all are running on same thread.

Nikhil Agrawal
  • 47,018
  • 22
  • 121
  • 208
  • So i would need to run every timer in a seperate thread or is there a other solution. The timers are always running nonstop. – Jackz Apr 16 '12 at 11:37
  • Yes different thread for different timers should be the best solution for this. Use this http://stackoverflow.com/questions/5727023/start-a-timer-from-different-thread-in-c-sharp – Nikhil Agrawal Apr 16 '12 at 11:39
  • Or this http://stackoverflow.com/questions/1416803/system-timers-timer-vs-system-threading-timer – Nikhil Agrawal Apr 16 '12 at 11:41
0

Use System.Threading.Timer and when updating UI use the SyncronizationContext.Syncronize to run the updating code in.

Don't forget to get the context from SyncronizationContext.Current on the UI thread.

NPehrsson
  • 1,548
  • 18
  • 26
  • Care to illustrate that a little more? Thanks. The Syntax of System.Timer seems more alike the Dispatcher then the Threading timer. – Jackz Apr 16 '12 at 12:29