Live Chat Icon For mobile
Live Chat Icon

What are the implications of using the Timer class in System.Timers as opposed to the Timer class in System.Windows.Forms

Platform: WinForms| Category: Tips

Be careful when using the Timer from System.Timers from within a Windows.Form application. Most methods in Windows Forms are not thread safe and can produce indeterminate results when called from another thread. In this case, System.Timers.Timer is much higher resolution and less affected by how busy your application is (since it doesn’t use the message pump) but it’s Tick event is fired from another thread. In order to be sure this works
correctly, you’ll have to write the proper Control.Invoke code to marshal over to the thread the control was created on (the main thread) before you call methods on the control. So, in cases where you don’t need super-high-resolution and regular events to be fired, you’re much better off using System.Windows.Forms.Timer. It’s message pump is based (WM_TIMER) and will fire on the UI thread.

(from on microsoft.public.dotnet.framework.windowsforms)

Share with

Related FAQs

Couldn't find the FAQs you're looking for?

Please submit your question and answer.