.NET Framework Bookmark and Share   
 index > .NET Base Class Library > Timer in C#
 

Timer in C#

Hi all,

I have tried to find an answer for my question on searching but I have not got a satisfied answer yet because this question requires experience about .net. Thanks in advance,

For my C# application I use 3 timers:
1. Timer 1: Tick every second (1000 ms), inside tick method, a counter_second++: thsi timer never stops
2. Timer 2: Tick every 20 ms is used for sending a command to serial port every 20 ms, and then stops to wait to receive reply
3. Timer 3: Tick every 100 ms: also never stops to check whether there is any response command from timer2 or not, if there is not, Retry to send from timer2 starts again.

I really dont understand clearly about timer, so my question is that if I use 3 timers like this, how about deadlock any other problems for this? because I think 3 timers are too much for one application, is it it?

Above 3 timer, I used Windows.Forms.Timer.

The second question is which one of following timers is the best to use:
System.Windows.Forms.Timer
System.Timers.Timer
System.Threading.Timer

with criteria such as:
1. perfomanace
2. memory
3. time consuming


Thanks in advance,

Joesy


nguyenxh
The callback function of the WinForms.Timer was executed in the main UI thread, and its executed when Windows send message(when a registered timer arrived) to the message queue of the current window's running thread. So if the three timers all fired, the callback functions' invocation will be blocked and called one after another. If one of the functions hangs, the consequential function invocation will be ignored.

System.Windows.Forms.Timer vs. System.Threading.Timer vs. System.Timers.Timer represents a detailed comparison of the three Timers.

Thanks!

Feng Chen
--deadlock will occur here - I assume you're talking about Windows Forms Timer.
No, the timer arrived in the form of window message in the UI thread which can only execute a task at the same time. Here, the timer2's task this time will wait for the finish of timer1's task.

The timer events raised by this timer class are synchronous with respect to the rest of the code in your Windows Forms app. This means that application code that is executing will never be preempted by an instance of this timer class (assuming you don't call Application.DoEvents).

By the way, the system will merge more than one WM_TIMER message into one.

And recommended reading the following article:
Comparing the Timer Classes in the .NET Framework Class Library
Feng Chen
The callback function of the WinForms.Timer was executed in the main UI thread, and its executed when Windows send message(when a registered timer arrived) to the message queue of the current window's running thread. So if the three timers all fired, the callback functions' invocation will be blocked and called one after another. If one of the functions hangs, the consequential function invocation will be ignored.

System.Windows.Forms.Timer vs. System.Threading.Timer vs. System.Timers.Timer represents a detailed comparison of the three Timers.

Thanks!

Feng Chen
Dear Feng Chen,

Thank you very much for your reply, it is very helpful though.

I still wonder:

I assume every job that every timer has to complete is 100%.

if timer1 is executing its job till 60%, then at this time timer2 ticks and wanna do its job but timer1 is executing ==> deadlock will occur here, so my question is that after timer completed, will timer2 continue with the waiting job?

Thanks in advance,

Kind regards,

Joesy
nguyenxh
--deadlock will occur here - I assume you're talking about Windows Forms Timer.
No, the timer arrived in the form of window message in the UI thread which can only execute a task at the same time. Here, the timer2's task this time will wait for the finish of timer1's task.

The timer events raised by this timer class are synchronous with respect to the rest of the code in your Windows Forms app. This means that application code that is executing will never be preempted by an instance of this timer class (assuming you don't call Application.DoEvents).

By the way, the system will merge more than one WM_TIMER message into one.

And recommended reading the following article:
Comparing the Timer Classes in the .NET Framework Class Library
Feng Chen

You can use google to search for other answers

Custom Search

More Threads

• CSharpCodeProvider and #line pragma/compiler directive
• What happens when you change a property?
• COMException was caught - The operation failed
• Directory.GetFiles with searchPattern to get all *.dll and *.exe files in one call
• Validate if a file exists in my Resources Folder
• accessing methods from arraylist
• Is it possible to make notification to TextWriter/StringWriter whenever content changes? similar to TextBox_TextChanged Event.
• Does the "Directory.Exists()" method REALLY NOT work when given a valid ftp address?
• how to get registry access permissions
• Encryption Errors