定时器会创建一个新线程吗?

发布于 2024-10-20 20:22:52 字数 173 浏览 1 评论 0原文

        timer.Interval = 5000;
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();

“timer_Tick”方法是在新线程中启动还是仍在创建它的线程中?

        timer.Interval = 5000;
        timer.Tick += new EventHandler(timer_Tick);
        timer.Start();

Does "timer_Tick" method start in a new thread or is it still in the thread it was created in?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

挽清梦 2024-10-27 20:22:52

不,计时器在创建它的线程中运行。

我假设您正在谈论 System.Windows.Forms.Timer 这是使用线程消息循环实现的。 WinForms 计时器的底层是 Win32 API SetTimer()它通过将 WM_TIMER 消息发布到 SetTimer() 线程的消息队列来进行操作。

这样做的后果之一是,如果您的事件处理程序花费的时间比计时器间隔长,那么您的计时器将不会按所需的间隔触发。如果这是一个问题,那么您需要将计时器放在另一个线程中。

作为一个思想实验,想象一下如果您的计时器事件确实在不同的线程中执行,会发生什么。现在您有一个同步问题需要处理。您的计时器事件可能想要从其他线程访问对象。但这样做会导致竞争条件。

No, a timer runs in the thread in which it was created.

I'm assuming you are talking about System.Windows.Forms.Timer which is implemented using the thread message loop. Underlying a WinForms timer is the Win32 API SetTimer() which operates by posting WM_TIMER messages to the message queue of the thread which SetTimer().

One of the consequences of this is that if you have an event handler that takes longer than your timer interval then your timer will not fire at the desired interval. If this was a problem then you'd need to house your timer in another thread.

As a thought experiment, imagine what would happen if your timer event did execute in a different thread. Now you have a synchronisation problem to handle. Your timer event is likely to want to access objects from the other thread. But to do so will result in race conditions.

执笏见 2024-10-27 20:22:52

计时器并没有真正“运行”。也就是说,当您启动计时器时,操作系统会创建一些数据结构,告诉它定期发出“滴答声”——无论您指定什么时间段。但这并不像计时器坐在那里旋转,在等待适当的时间时消耗 CPU 资源。所有 .NET 计时器类型和 Windows API 计时器类型都以这种方式工作。

不同之处在于,当需要进行勾选时会发生什么。正如 @David Hefferman 指出的,使用 System.Windows.Forms.Timer,Elapsed 事件处理程序在创建计时器的同一线程上调用。 System.Threading.Timer 在线程池线程上调用其回调。在后台,System.Timers.Timer 是在池线程上调用的,但您可以使用 SynchronizingObject 属性来引发 Elapsed 事件UI 线程或任何其他线程。

A timer doesn't really "run". That is, when you start a timer, the operating system creates some data structures that tell it to issue a "tick" periodically--at whatever period you've specified. But it's not like the timer is sitting there spinning, eating CPU resources while it waits for the proper time. All of the .NET timer types and the Windows API timer types work this way.

The difference is in what happens when it comes time to make a tick. As @David Hefferman pointed out, with System.Windows.Forms.Timer, the Elapsed event handler is called on the same thread that created the timer. System.Threading.Timer calls its callback on a thread pool thread. Under the hood, System.Timers.Timer is called on a pool thread, but you can use the SynchronizingObject property to raise the Elapsed event on the UI thread or any other thread.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文