是否System.Windows.Forms.Timer在不同的线程比UI运行? [英] Does the System.Windows.Forms.Timer run on a different thread than the UI?

查看:538
本文介绍了是否System.Windows.Forms.Timer在不同的线程比UI运行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个创建创建并设置一个计时器来运行()每分钟名为updateStatus功能的表单对象一个主线程。但updateStatus()也是在几个地方调用主线程。

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.

不过,我并不清楚是否会造成任何同步问题。是否System.Windows.Forms.Timer在C#运行在不同的线程比主线程其他的?

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?

推荐答案

没有,定时器事件提出的UI线程上。

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

您不会有任何同步问题。这是正确的版本定时器控制在一个WinForms应用程序中使用的;它是专门用来做什么你问。它使用标准的的Windows计时器引擎盖下实现的。

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:

一个定时器用来引发事件在用户定义的时间间隔。此视窗定时器被设计用于其中的UI线程用于执行处理单线程环境。它要求用户code有一个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.

在使用此计时器,使用Tick事件进行轮询操作或显示一个启动画面指定的时间段。每当Enabled属性设置为true,并且Interval属性大于零,Tick事件引发的基础上,Interval属性设置的时间间隔。

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.

这篇关于是否System.Windows.Forms.Timer在不同的线程比UI运行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆