I have a legacy WinForms Mdi App in VB.Net 2.0 which I am adding functionality to. One of the additions is a warning which needs to be raised when the current time nears a specified value (a deadline). My intention is to check the time once an hour until there is less than an hour until the deadline, then display warnings at specified intervals until the time's up.
The user needs to be able to continue to use the app up to and even after the deadline, but they need to periodically be made aware of the deadline's proximity.
The app does not use System.Threading yet and my knowledge of it is limited at this time. I do know that there are 3 different Timer() methods available:
My question is, which is the best way to go with this? I attempted to use the threaded timer, but since WinForms are not thread safe I got a run time error trying to access another class. Is it worth making the class/form thread safe? Am I completely off track?
Thanks.
Timers. Timer is geared towards multithreaded applications and is therefore thread-safe via its SynchronizationObject property, whereas System.
Elapsed event, creates a timer, and starts the timer. The event handler has the same signature as the ElapsedEventHandler delegate. The event handler displays the SignalTime property each time it is raised.
No, a timer runs in the thread in which it was created.
This article explains pretty well:
Comparing the Timer Classes in the .NET Framework Class Library
It sounds like System.Windows.Forms.Timer is the one for you.
My guideline: If you want the timer to run on your main GUI thread, stick with Windows.Forms.Timer. If it's okay for your timer to be called asynchronously on a thread pool thread, or if you don't want to experience the small delays that System.Windows.Forms.Timer tends to suffer, use System.Timers.Timer. System.Threading.Timer has a different interface from the other two and is not thread-safe; personally, I'm not a fan.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With