系统.线程.定时器
当我们使用System.Threading.Timer时,那么该方法是在创建计时器的线程上执行的吗?或者 ir 是在另一个线程中执行的?
class Timer
{
static void Main()
{
TimerCallback tcall = statusChecker.CheckStatus;
Timer stateTimer = new Timer(tcb, autoEvent, 1000, 250);
}
}
class StatusChecker
{
public void CheckStatus(Object stateInfo)
{
}
}
我的问题是计时器委托(CheckStatus
)调用的方法是在主线程中执行还是在另一个线程中执行?
When we use System.Threading.Timer, then is the method executed on the thread that created the timer? Or is ir executed in another thread?
class Timer
{
static void Main()
{
TimerCallback tcall = statusChecker.CheckStatus;
Timer stateTimer = new Timer(tcb, autoEvent, 1000, 250);
}
}
class StatusChecker
{
public void CheckStatus(Object stateInfo)
{
}
}
My question is if the method called by the timer delegate (CheckStatus
) is executed in main thread or is it executed in another thread?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
System.Threading.Timer
将在线程池中的另一个线程上执行其工作。System.Windows.Forms.Timer
将在现有 (GUI) 线程上执行。System.Threading.Timer
will execute its work on another thread in the thread pool.System.Windows.Forms.Timer
will execute on the existing (GUI) thread.文档说如下:
所以回调几乎肯定会在另一个线程上。
当然,如果您从 ThreadPool 线程启动计时器,它有可能在同一线程上执行,但不能保证。
The docs say the following:
So the callback will almost certainly be on another thread.
Of course, if you launch the timer from a ThreadPool thread, there's a chance it might execute on the same thread, but no guarantee.
MSDN 指出:
因此,在您的示例中,计时器委托
(CheckStatus)
将在单独的线程中执行。MSDN States:
Hence, in your example, timer delegate
(CheckStatus)
would be executed in an seperate thread.