C# 计时器是否在单独的线程上消逝? [英] Do C# Timers elapse on a separate thread?

查看:22
本文介绍了C# 计时器是否在单独的线程上消逝?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

System.Timers.Timer 是否在与创建它的线程不同的线程上运行?

Does a System.Timers.Timer elapse on a separate thread than the thread that created it?

假设我有一个带有定时器的类,每 5 秒触发一次.当计时器触发时,在 elapsed 方法中,一些对象被修改.假设修改这个对象需要很长时间,比如 10 秒.在这种情况下,我有可能会遇到线程冲突吗?

Lets say I have a class with a timer that fires every 5 seconds. When the timer fires, in the elapsed method, some object is modified. Lets say it takes a long time to modify this object, like 10 seconds. Is it possible that I will run into thread collisions in this scenario?

推荐答案

对于 System.Timers.Timer:

请参阅下面布赖恩·吉迪恩 (Brian Gideon) 的回答

对于 System.Threading.Timer:

关于计时器的 MSDN 文档 指出:

System.Threading.Timer 类 使得ThreadPool 线程上的回调 和根本不使用事件模型.

The System.Threading.Timer class makes callbacks on a ThreadPool thread and does not use the event model at all.

因此计时器确实在不同的线程上结束.

So indeed the timer elapses on a different thread.

这篇关于C# 计时器是否在单独的线程上消逝?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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