Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why there are 5 Versions of Timer Classes in .NET?

Tags:

c#

.net

timer

Why are there five timer classes in the .Net framework, namely the following:

  1. System.Timers.Timer
  2. System.Threading.Timer
  3. System.Windows.Forms.Timer
  4. System.Web.UI.Timer
  5. System.Windows.Threading.DispatcherTimer

Why are there several versions of the Timer class? And what are the differences between them?

like image 558
Mohammed A. Fadil Avatar asked Apr 25 '12 13:04

Mohammed A. Fadil


People also ask

What is the purpose of the AutoReset property of the timer in C #?

If the Timer is already enabled when the Start method is called, the interval is reset. If AutoReset is false , the Start method must be called in order to start the count again. Resetting the interval affects when the Elapsed event is raised.

What is the use of timer in C#?

C# Timer is used to implement a timer in C#. The Timer class in C# represents a Timer control that executes a code block at a specified interval of time repeatedly. For example, backing up a folder every 10 minutes, or writing to a log file every second.

Which of the following is a function of the timer class?

Timer class provides a method call that is used by a thread to schedule a task, such as running a block of code after some regular instant of time.

Is timer a thread C#?

The Timer class provides a way to execute methods at specified intervals and it cannot be inherited. The Timer class (in the System. Threading namespace) is effective to periodically run a task on a separate thread. It provides a way to execute methods at specified intervals.

What are the different types of timers in the class library?

The .NET Framework Class Library provides three different timer classes: System.Windows.Forms.Timer, System.Timers.Timer, and System.Threading.Timer. Each of these classes has been designed and optimized for use in different situations.

What is the timer class in the NET Framework?

The .NET Framework documentation refers to the System.Timers.Timer class as a server-based timer that was designed and optimized for use in multithreaded environments. Instances of this timer class can be safely accessed from multiple threads.

What is the use of timer in Windows Forms?

Like the System.Timers.Timer class, this class is intended for use as a server-based or service component in a multithreaded environment; it has no user interface and is not visible at runtime. System.Windows.Forms.Timer (.NET Framework only): a Windows Forms component that fires an event at regular intervals.

What is the best way to write timer-driven managed code?

Writing effective timer-driven managed code requires a clear understanding of program flow and the subtleties of the .NET threading model. The .NET Framework Class Library provides three different timer classes: System.Windows.Forms.Timer, System.Timers.Timer, and System.Threading.Timer.


2 Answers

Here's a description of the primary timers and the points that i find to be the most noteworthy.

Winforms.Timer

  • ticks on UI thread not guaranteed to ticket at a specific time
  • ticks delayed until UI thread is idle
  • will skip ticks if the UI thread is busy

DispatcherTimer

  • invoked on UI thread
  • can set priority for what level of 'idle' is required to generate a tick
  • will skip ticks

Threading.Timer

  • ticks on a worker thread from threadpool - no option for specifying thread
  • ticks are always fired on time
  • none are skipped - you must guard against new ticks while you're still processing a former tick
  • unhandled exceptions will crash the application

Timers.Timer

  • wrapper around threading timer
  • ticks on a worker thread taken from the CLR threadpool
  • can force to tick on a specific thread by supplying a SynchronizationObject
  • ticks are always fired on time
  • none are skipped
  • silently eats exceptions
like image 150
Bill Tarbell Avatar answered Oct 05 '22 10:10

Bill Tarbell


Timers.Timer generates an event after a set interval, with an option to generate recurring events. MSDN

Windows.Forms.Timer is a Control for winforms.

Web.UI.Timer performs asynchronous or synchronous Web page postbacks at a defined interval. MSDN

Threading.Timer is the timer for Callbacks. Creates a new Thread for working. Served by thread pool threads. MSDN

So, these timers have different purposes, also they are served by different tools.

like image 35
Victor Gorban Avatar answered Oct 05 '22 12:10

Victor Gorban