2

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.

Ali
  • 1,801
  • 6
  • 43
  • 58

3 Answers3

5

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.

Andrew Barber
  • 39,603
  • 20
  • 94
  • 123
  • So if I have understood correctly, you mean to say that once the operation is being done, the Timer control will wait until it is complete and then execute again rather than not waiting for operation to finish and execute again anyway? By the way I am using Timer control in a console application in a separate thread. – Ali Jan 25 '12 at 04:57
  • Actually, what you say now could change everything. When you say "timer control" do you mean the Timer that can be dragged and dropped onto a Windows Form? What is the full namespace of it? When you say "control", I assumed you were using that, in a Windows Forms application. – Andrew Barber Jan 25 '12 at 05:00
  • Oh I am using "System.Timers.Timer()" – Ali Jan 25 '12 at 05:06
  • 2
    That's exactly I suspected that the Timer is running overlapping operations because of which I was getting unexpected behavior. Based on your input I have modified my code (I am using point 2 of your suggestion to ENABLE and DISABLE timer) and now it works perfectly. I have marked your answer as accepted. Thanks. – Ali Jan 25 '12 at 05:32
0

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?

Community
  • 1
  • 1
Martin Samson
  • 3,970
  • 21
  • 25
0

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.

Karthik
  • 187
  • 3
  • 11