What are the differences between System.Threading.Timer and creating your own background thread in C #

I have a background function that is structured as follows:

System.Threading.Thread myTimerThread = new Thread(this.Tick); private void Tick(){ do{ //do stuff System.Threading.Sleep(1000L); }while(true) } 

However, there is a System.Threading.Timer class that does this for me. What are the differences in using the Timer built-in class present in System.Threading instead of creating my own background thread with the Tick function?

+1
source share
3 answers

The Timer class will be very lightweight and more efficient than your own dedicated thread, which sleeps for a certain amount of time inside an infinite while loop.

Read Thread.Sleep - This is a sign of a poorly designed program to find out how Thread.Sleep works and how it completely destroys thread and resources.

System.Threading.Timer, on the other hand, will use the ThreadPool thread to execute the timer. Another advantage of using the Timer class, as described in MSDN

When creating a timer, you can specify the wait time before the first execution of the method (execution time) and the amount of waiting time between subsequent executions (period). You can change these values โ€‹โ€‹or disable the timer using the "Change" method.

You wonโ€™t have these benefits in a streaming approach.

+4
source

First of all, use a thread pool if you are not performing a lengthy operation. The difference between your own timer and System.Threading.Timer is that System.Threading.Timer uses hardware interrupts to know when it is appropriate to execute a tick. It will be more accurate (although the multimedia timer will be even more accurate) than just sleeping in milliseconds, which will have to wait until the thread scheduler provides control before your thread has control.

You should also know that if you are doing something that will affect Gui in your stream, you must use the appropriate version of the Gui timer, otherwise your ticks will not occur in the stream that you must connect to, and you will need to call, to get into the correct thread. For window forms, this is System.Windows.Forms.Timers, this is System.Windows.Threading.DispatcherTimer for WPF and Silverlight. For more information on threads and timers, I highly recommend Joseph Albahariโ€™s free e-book Threading in C # .

0
source

You have more control with System.Threading.Timer. You can program a timer to check for a specific thread or event that everyone will say .... 1/4 of a second until it starts, and then you can recycle the timer using the dispose method. This is much more flexible because you can program it to do whatever you want, and it is much more accurate.

When you use Thread.Sleep, you really only have one parameter, and this forces the program to "sleep" in x seconds. As far as I know, you cannot dispose of it, time, coordinate it, so that it stops earlier. etc. The bottom line is that even after completing your program, Thread.Sleep will continue to make the program sleep. Threading.Timer can be programmed to stop when the program terminates.

0
source

All Articles