I am trying to port some code from C++ to C#.
I came across this in the C++ code:
watchdogTimer = SetTimer(1,1000,NULL);
...
KillTimer(watchdogTimer);
What is this code doing, and how can this be ported to C#?
Thanks.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
The
CWnd::SetTimerfunction you’re looking at creates a timer that sendsWM_TIMERevents to the window. This is analogous to the System.Windows.Forms.Timer component in .NET. It behaves somewhat differently than theSystem.Timers.Timer. There are two differences that are particularly relevant:Windows.Forms.Timercalls the event handler on the UI thread. By default,System.Timers.Timercalls the event handler on a threadpool thread. You can use SynchronizingObject property to have theSystem.Timers.Timercall on the UI thread.Another difference is that it’s not possible to encounter reentrancy problems with the Windows Forms timer because Windows won’t allow multiple
WM_TIMERmessages from the same timer in the queue, nor will it place aWM_TIMERmessage in the queue if one is already being processed. This is generally a good thing.System.Timers.Timer, on the other hand, will allow reentrancy. So if your timer event handler takes longer than the timer period, you can be processing multiple events for the same timer concurrently. If your timer period is 100 ms and processing takes 150 ms, you’re going to get another notification while you’re processing the first one. If you use theSynchronizingObjectto force the callback on the UI thread, this can lead to a whole bunch of pending callbacks being queued.The implementation of the two timers is quite different. The Windows Forms timer uses old style Windows timers that have been around for 20 years. This type of timer requires a window handle and a message loop, and is therefore used only in GUI programs.
System.Timers.Timeris a thin wrapper aroundSystem.Threading.Timer, which uses the Windows Thread Pool Timers.