26

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,738
  • 21
  • 105
  • 131
user186246
  • 1,857
  • 9
  • 29
  • 45

6 Answers6

26

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 - on strike
  • 239,200
  • 50
  • 490
  • 574
  • 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 - on strike Apr 18 '11 at 04:45
9

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
  • 922,412
  • 146
  • 1,693
  • 2,536
5

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.

H H
  • 263,252
  • 30
  • 330
  • 514
4

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
  • 1,026,079
  • 266
  • 2,566
  • 2,900
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
  • 1,023,142
  • 271
  • 3,287
  • 2,928
-2

I don't want to impeach the reputation of others how has answered, that Windows.Forms.Timer does always run on GUI thread, but I have to contradict. :)

I would answer - it depends. It can be, that Windows.Forms.Timer does run on Non-GUI thread!

Windows.Forms.Timer does fire it's Timer.Tick event on the thread, where the Form, which contains the timer, has been created.

If the form is created on the GUI thread, then the Timer.Tick runs on GUI thread, if however Form is created on the e.g. some thread XXX from thread pool(what you should avoid), then the Timer.Tick runs on this XXX thread.

Task.Run(()=>
{
    using (var formWithTimer = new SomeFormWithTimer()) //Form created on worker thread, Tick handled on it.
    {
        formWithTimer.ShowDialog();
    }
});
Rekshino
  • 6,954
  • 2
  • 19
  • 44
  • @HenkHolterman Do you mean, that my answer is wrong? – Rekshino Sep 27 '22 at 20:50
  • @HenkHolterman or do you agree, that `Timer.Tick` can under circumstances run on Non-GUI thread ;)? – Rekshino Sep 27 '22 at 20:54
  • It's not my problem, in answer it's also addressed, that it shouldn't be done. The answer is here for the complement to emphasize, that `Timer.Tick` not mandatory runs on GUI thread, even if it's not a realistic scenario. – Rekshino Sep 29 '22 at 20:05