System.Timers.Timer是否在独立线程中运行? [英] Do System.Timers.Timer run in independent Threads?

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

问题描述

我想了解 System.Timers.Timer 何时引发已发生的事件,它是否在独立线程中引发?

I'm trying to understand when the System.Timers.Timer raises the elapsed event, is it raised in an independent thread?

下面的示例似乎表明三个计时器在各自的线程中独立运行:

My example below seems to suggest that the three timers run independently in their own threads:

class Program
{
    static System.Timers.Timer timer = new System.Timers.Timer();
    static System.Timers.Timer timer2 = new System.Timers.Timer();
    static System.Timers.Timer timer3 = new System.Timers.Timer();

    static void Main(string[] args)
    {
        timer.Elapsed += new System.Timers.ElapsedEventHandler(
            timer_Elapsed);
        timer2.Elapsed += new System.Timers.ElapsedEventHandler(
            timer2_Elapsed);
        timer3.Elapsed += new System.Timers.ElapsedEventHandler(
            timer3_Elapsed);

        timer.Interval = 1000;
        timer2.Interval = 1000;
        timer3.Interval = 1000;

        timer.Start();
        timer2.Start();
        timer3.Start();

        Console.WriteLine("Press \'q\' to quit the sample.");
        while (Console.Read() != 'q') ;
    }

    static void timer3_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        timer3.Stop();
        Console.WriteLine("Timer 3 Hit...");            
        timer3.Start();
    }

    static void timer2_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        timer2.Stop();
        Console.WriteLine("Timer 2 Hit...");
        Thread.Sleep(2000);
        timer2.Start();
    }

    static void timer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
    {
        timer.Stop();
        Console.WriteLine("Timer 1 Hit...");
        Thread.Sleep(10000);
        timer.Start();
    }
}

推荐答案

根据MSDN, System.Timers.Timer Elapsed 事件触发时,在系统线程池中的线程上调用它: / p>

According to the MSDN, on System.Timers.Timer when the Elapsed event fires it is called on a thread in the system thread-pool:


如果SynchronizingObject属性为Nothing,则在ThreadPool线程上引发Elapsed事件。如果对Elapsed事件的处理持续时间长于Interval,则该事件可能在另一个ThreadPool线程上再次引发。在这种情况下,事件处理程序应该是可重入的。

If the SynchronizingObject property is Nothing, the Elapsed event is raised on a ThreadPool thread. If processing of the Elapsed event lasts longer than Interval, the event might be raised again on another ThreadPool thread. In this situation, the event handler should be reentrant.

由于默认值 SynchronizingObject 为null,那么所有经过的事件都将在线程池上处理。因此,这取决于线程池的填充量,如果有空闲线程,则每个经过的事件很可能可以在单独的线程上同时运行。但是,如果由于某种原因,系统线程池已被完全使用,则可以将经过的事件按计划进行序列化。

Since the default value of SynchronizingObject is null, then all your elapsed events would be handled on the thread pool. So, it depends how full the thread pool is, if there are free threads, then each elapsed event can most likely run concurrently on separate threads. If for some reason, though, the system thread-pool is already fully in use, it's possible the elapsed events could be serialized as they are scheduled.

要点是:取决于情况。也就是说,只要池中有可用线程,它们就可以并行运行。

The main point is: "it depends." That is, they will be allowed to run in parallel as long as there are free threads in the pool.

参考:System.Timers.Timer上的MSDN

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

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