Understanding working of Timer control in C#

2019-05-28 14:47发布

问题:

Let's say I have a Timer control with interval set to 20ms. Inside this control I am doing some operation that takes 100ms to complete. So once it is doing that operation, will Timer control execute again without waiting for that operation to finish? Or until that operation is not finished, Timer will not execute again?

EDIT
I am using System.Timers.Timer() in a separate thread in console application. I have edited my question after discussing with Andrew below.

回答1:

System.Timers.Timer both runs and raises events on a 'random', thread pool thread. The callbacks and the timer ticks will run concurrently, meaning you would have overlapping execution of these events in your situation where the interval is less than the time the method takes to terminate.

Even though this behavior is different from that which I posted about below, you still don't want to do this. The result will be that the thread pool will eventually become exhausted. It is also probably not your desired functionality.

A solution would be to code it such that concurrent execution is not possible.

  1. Test some static field to see if an existing iteration is running, and cancel the 'new' instance if so. Testing/setting such a field should be done through a memory barrier.
  2. Stop the timer upon entering the event, run the code, and then start it again after. If you wish, you can adjust the interval based on how long it took to execute.
  3. Do not do simple locking to block concurrent executions until former ones are done; that will just go back to the problem I described with the Windows Forms Timer (minus the UI lockup)

Previous Answer

This was the old answer I posted when I thought you were speaking of the Timer in System.Windows.Forms and using it in a Windows Forms program. Because that is not the case, the information below does not apply to you. But I leave it here in case it helps someone else.

Because the Timer control inside the System.Windows.Forms namespace runs on the UI thread, as does its callback, your tick events will pile up, waiting for currently executing operations to complete. You UI will also be locked in the duration.

Therefore, you do not want to do this. Your program will lock up.



回答2:

The System.Windows.Forms.Timer is single-threaded and only has a resolution of 55ms.But since it's single-threaded the Tick events shouldn't step on each other.

http://msdn.microsoft.com/en-us/library/system.windows.forms.timer.aspx



回答3:

In a single threaded application, ticks will be queued if the execution could not happen. It also has a resolution of ~55ms

See http://msdn.microsoft.com/en-us/library/xdh6857z.aspx

See How to prevent System.Timers.Timer from queuing for execution on a thread pool?



回答4:

System.Timers.Timer

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. Unlike the System.Windows.Forms.Timer, the System.Timers.Timer class will, by default, call your timer event handler on a worker thread obtained from the common language runtime (CLR) thread pool.

For more details on timer types, please refer the link http://msdn.microsoft.com/en-us/magazine/cc164015.aspx

Note : The previous example was written assuming that the timer instance was System.Windows.Forms.Timer but clarifications from the author indicated that System.Timers.Timer() instance is used.



标签: c# .net timer