System.Timers.Timer / Threading.Timer vs Thread with WhileLoop + Thread.Sleep for periodic tasks

In my application, I have to send periodic beats to the brother application.

Is this improved with System.Timers.Timer / Threading.Timer or with Thread with a while loop and Thread.Sleep?

The heart rate interval is 1 second.

while(!exit) { //do work Thread.Sleep(1000); } 

or

 myTimer.Start( () => { //do work }, 1000); //pseudo code (not actual syntax)... 
+43
multithreading c # timer
May 12, '10 at 20:16
source share
4 answers

System.Threading.Timer has my vote.

System.Timers.Timer is intended to be used on the server (your code works as a server / service on the host machine, not as a user).

A thread with a While loop and Thread.Sleep is a really bad idea, given the existence of more robust Timer mecahnisms in .NET.

+32
May 12, '10 at 20:17
source share

Server timers are a different creature than dormant threads.

On the one hand, depending on the priority of your thread and what else works, your sleep thread may or may not be awakened and scheduled to run at the interval you requested. If the interval is long enough and scheduling accuracy is not a big deal, Thread.Sleep() is the smart choice.

Timers, on the other hand, can create their own events in any thread, which improves planning capabilities. The cost of using timers, however, is a bit more complicated in your code - and the fact that you cannot control which thread executes the logic in which the timer event fires. From the docs:

The server timer is designed to be used with workflows in a multi-threaded environment. server timers can move between threads to handle the raised Expired event, which leads to greater accuracy than Windows Timers when increasing time.

Another consideration is that timers call their delegated delegate in the ThreadPool thread. Depending on the time and complexity of your logic, you may not want to run it in the thread pool - you may need a dedicated thread. Another factor with timers is that if the processing takes a lot of time, the timer event can be raised again (at the same time) on another thread - which can be a problem if the executed code is not designed or structured for concurrency.

Do not confuse server timers with Windows Timers . Later, they usually refer to WM_TIMER messages that can be delivered to a window, which allows the application to schedule and respond to temporary processing on its main thread without sleep. However, Windows timers can also refer to Win API for low level time (this is not the same as WM_TIMER).

+23
May 12 '10 at 20:18
source share

None :)

The sleepman usually frowned (unfortunately, I don’t remember the details, but firstly, it’s an uncaught block), and the Timer comes with a lot of luggage. If possible, I would recommend System.Threading.AutoResetEvent as such

 // initially set to a "non-signaled" state, ie will block // if inspected private readonly AutoResetEvent _isStopping = new AutoResetEvent (false); public void Process() { TimeSpan waitInterval = TimeSpan.FromMilliseconds (1000); // will block for 'waitInterval', unless another thread, // say a thread requesting termination, wakes you up. if // no one signals you, WaitOne returns false, otherwise // if someone signals WaitOne returns true for (; !_isStopping.WaitOne (waitInterval); ) { // do your thang! } } 

Using AutoResetEvent (or its cousin ManualResetEvent ) guarantees a true block with thread safety (for things like graceful completion above). In the worst case, this is the best alternative to Sleep

Hope this helps :)

+21
May 12 '10 at 20:27
source share

I found that the only timer implementation that actually scales is System.Threading.Timer . All other implementations seem rather fictitious if you are dealing with a non-trivial number of planned elements.

+1
May 12 '10 at 20:28
source share



All Articles