Different timers in .net

时间:2023-03-10 06:05:50
Different timers in .net
  • Multi-threads timers: System.Threading.Timer and System.Timers.Timer (.net framework):

App will handle timer event on a thread pool thread. Timers.Timer is a wrapper for Threading.Timer. They are used for doing time-consuming tasks.

If a System.Timers.Timer is used in a WPF application, it is worth noting that the System.Timers.Timer runs
on a different thread then the user interface (UI) thread.

In order to access objects on the user interface (UI) thread, it is necessary to post the operation onto the Dispatcher of
the user interface (UI) thread using Invoke or Begi

nInvoke.

Reasons for using a DispatcherTimer opposed to a System.Timers.Timer are
that theDispatcherTimer runs on the same thread as the Dispatcher and
DispatcherPrio

rity can be set on the DispatcherTimer.

  • Single-thread timers: System.Windows.Forms.Timer(Windows Forms Timer)and System.Windows.Threading.DispatcherTimer(WPF timer)

Based on Windows message loop, app will handle timer event synchronously, so they may block UI.They are called in the same thread as the
application. They are

used for small tasks such as updating UI.