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.
我有一个主线程创建一个表单对象,它创建并设置一个计时器,每分钟运行一个名为updateStatus()的函数。但是updateStatus()也被主线程在几个地方调用。
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?
但是,我不清楚它是否会导致任何同步问题。 C#中的System.Windows.Forms.Timer是否运行在主线程以外的其他线程上?
5 个解决方案
#1
21
No, the timer events are raised on the UI thread.
不,在UI线程上引发了计时器事件。
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.
你不会有任何同步问题。这是在WinForms应用程序中使用的正确版本的计时器控件;它专门设计用于满足您的要求。它是使用标准的Windows计时器在引擎盖下实现的。
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.
Timer用于以用户定义的间隔引发事件。此Windows计时器专为使用UI线程执行处理的单线程环境而设计。它要求用户代码具有可用的UI消息泵并且始终在同一线程中操作,或者将调用编组到另一个线程上。
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.
使用此计时器时,请使用Tick事件执行轮询操作或在指定的时间段内显示闪屏。只要Enabled属性设置为true且Interval属性大于零,就会根据Interval属性设置按间隔引发Tick事件。
#2
8
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.
不,当收到WM_TIMER消息时,消息循环会从UI线程引发计时器的Tick事件。你总是很擅长,它只能在你的UI线程空闲时运行。
#3
6
No.
没有。
The whole point of a Windows.Forms Timer is that it runs on the GUI Thread.
Windows.Forms Timer的重点在于它在GUI Thread上运行。
Windows (WinForms) runs something called the MessagePump (see Application.Run()
) and this is what makes the Timer possible.
Windows(WinForms)运行一个名为MessagePump的东西(参见Application.Run()),这就是使Timer成为可能的原因。
All your code runs as part of an Eventhandler somehow, and a Timer tick will never 'interrupt' any other event handler.
所有代码都以某种方式作为Eventhandler的一部分运行,并且Timer tick将永远不会“中断”任何其他事件处理程序。
#4
5
The Windows.Forms timer raises the event back on the UI thread, presumably via the sync-context.
Windows.Forms计时器在UI线程上引发事件,可能是通过sync-context。
If you want a non-UI thread, there are other timers - for example System.Timers.Timer or System.Threading.Timer
如果你想要一个非UI线程,还有其他定时器 - 例如System.Timers.Timer或System.Threading.Timer
#5
2
According to the documentation it runs on the main UI thread:
根据它在主UI线程上运行的文档:
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.
Timer用于以用户定义的间隔引发事件。此Windows计时器专为使用UI线程执行处理的单线程环境而设计。它要求用户代码具有可用的UI消息泵并且始终在同一线程中操作,或者将调用编组到另一个线程上。
#1
21
No, the timer events are raised on the UI thread.
不,在UI线程上引发了计时器事件。
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.
你不会有任何同步问题。这是在WinForms应用程序中使用的正确版本的计时器控件;它专门设计用于满足您的要求。它是使用标准的Windows计时器在引擎盖下实现的。
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.
Timer用于以用户定义的间隔引发事件。此Windows计时器专为使用UI线程执行处理的单线程环境而设计。它要求用户代码具有可用的UI消息泵并且始终在同一线程中操作,或者将调用编组到另一个线程上。
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.
使用此计时器时,请使用Tick事件执行轮询操作或在指定的时间段内显示闪屏。只要Enabled属性设置为true且Interval属性大于零,就会根据Interval属性设置按间隔引发Tick事件。
#2
8
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.
不,当收到WM_TIMER消息时,消息循环会从UI线程引发计时器的Tick事件。你总是很擅长,它只能在你的UI线程空闲时运行。
#3
6
No.
没有。
The whole point of a Windows.Forms Timer is that it runs on the GUI Thread.
Windows.Forms Timer的重点在于它在GUI Thread上运行。
Windows (WinForms) runs something called the MessagePump (see Application.Run()
) and this is what makes the Timer possible.
Windows(WinForms)运行一个名为MessagePump的东西(参见Application.Run()),这就是使Timer成为可能的原因。
All your code runs as part of an Eventhandler somehow, and a Timer tick will never 'interrupt' any other event handler.
所有代码都以某种方式作为Eventhandler的一部分运行,并且Timer tick将永远不会“中断”任何其他事件处理程序。
#4
5
The Windows.Forms timer raises the event back on the UI thread, presumably via the sync-context.
Windows.Forms计时器在UI线程上引发事件,可能是通过sync-context。
If you want a non-UI thread, there are other timers - for example System.Timers.Timer or System.Threading.Timer
如果你想要一个非UI线程,还有其他定时器 - 例如System.Timers.Timer或System.Threading.Timer
#5
2
According to the documentation it runs on the main UI thread:
根据它在主UI线程上运行的文档:
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.
Timer用于以用户定义的间隔引发事件。此Windows计时器专为使用UI线程执行处理的单线程环境而设计。它要求用户代码具有可用的UI消息泵并且始终在同一线程中操作,或者将调用编组到另一个线程上。