25

I have a main thread that creates a form object which creates and sets a timer to run a function named updateStatus() every minute. But updateStatus() is also called by the main thread at several places.

However, I am not clear about whether or not it will cause any synchronization problems. Does the System.Windows.Forms.Timer in C# run on a different thread other than the main thread?

Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
user186246
  • 1,767
  • 9
  • 27
  • 45

5 Answers5

27

No, the timer events are raised on the UI thread.

You won't have any synchronicity problems. This is the correct version of the timer control to use in a WinForms application; it's specifically designed to do what you're asking. It's implemented under the hood using a standard Windows timer.

The documentation confirms this in the Remarks section:

A Timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread.

When you use this timer, use the Tick event to perform a polling operation or to display a splash screen for a specified period of time. Whenever the Enabled property is set to true and the Interval property is greater than zero, the Tick event is raised at intervals based on the Interval property setting.

Community
  • 1
  • 1
Cody Gray
  • 230,875
  • 49
  • 477
  • 553
  • So the windows timer is running on the main thread, it will be always synchronous and I need not put lock block inside the updateStatus() function . Am I right? – user186246 Apr 18 '11 at 04:41
  • @user: Yes, that's correct. It runs just like any other event that gets raised on your form, such as the `Paint` or `Load` events. You don't need to lock in those handlers, either. – Cody Gray Apr 18 '11 at 04:45
10

No, the timer's Tick event is raised from the UI thread by the message loop when it receives a WM_TIMER message. You're always good with that, it can only run when your UI thread is idle.

Hans Passant
  • 897,808
  • 140
  • 1,634
  • 2,455
6

No.

The whole point of a Windows.Forms Timer is that it runs on the GUI Thread.

Windows (WinForms) runs something called the MessagePump (see Application.Run()) and this is what makes the Timer possible.

All your code runs as part of an Eventhandler somehow, and a Timer tick will never 'interrupt' any other event handler.

Henk Holterman
  • 250,905
  • 30
  • 306
  • 490
5

The Windows.Forms timer raises the event back on the UI thread, presumably via the sync-context.

If you want a non-UI thread, there are other timers - for example System.Timers.Timer or System.Threading.Timer

Marc Gravell
  • 976,458
  • 251
  • 2,474
  • 2,830
2

According to the documentation it runs on the main UI thread:

A Timer is used to raise an event at user-defined intervals. This Windows timer is designed for a single-threaded environment where UI threads are used to perform processing. It requires that the user code have a UI message pump available and always operate from the same thread, or marshal the call onto another thread.

Darin Dimitrov
  • 994,864
  • 265
  • 3,241
  • 2,902