WPF Manager timer timer disables my application

I have a little problem using WPF Dispatcher Timer. On each timer, the checkmark of my application freezes for a moment (until the timer method disappears). This is my code:

private DispatcherTimer _Timer = new DispatcherTimer(); _Timer.Tick += new EventHandler(_DoLoop); _Timer.Interval = TimeSpan.FromMilliseconds(1500); _Timer.Start(); 

Is there any way to avoid this and my application runs smoothly?

+4
source share
1 answer

Expected. your _DoLoop runs in the same thread as the user interface.

from DispatcherTimer Class MSDN

If System.Timers.Timer is used in WPF, it is worth noting that System.Timers.Timer works on a different thread, then the user interface (UI). In order for access objects in the user interface (UI), you must publish the operation on the Manager thread user interface (UI) using Invoke or BeginInvoke. The reasons for using DispatcherTimer, the opposite of System.Timers.Timer, is that DispatcherTimer works on the same thing as the dispatcher and DispatcherPriority can be set to DispatcherTimer.

If you need to perform time-consuming calculations, run it in another thread to support the user interface.

+6
source

Source: https://habr.com/ru/post/1307134/


All Articles