WPF Dispatchertimer delayed reaction / freeze

.net, c#, wpf

Solution

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

Problem

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.

Original source

Related problems