System.Threading.Timer实际上是在两个不同的线程中运行的吗?

时间:2016-12-17 03:52:35

标签: c# .net multithreading timer threadpool

下面的代码示例

using System.Threading;

namespace TimerApp
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("***** Timer Application *****\n");
            Console.WriteLine("In the thread #{0}",     Thread.CurrentThread.ManagedThreadId); 

            // Create the delegate for the Timer type. 
            TimerCallback timerCB = new TimerCallback(ShowTime);

            // Establish timer settings. 
            Timer t = new Timer(
                timerCB,                // The TimerCallback delegate object. 
                "Hello from Main()",    // Any info to pass into the called method (null for no info). 
                0,                      // Amount of time to wait before starting (in milliseconds). 
                1000);                  // Interval of time between calls (in milliseconds). 

            Console.WriteLine("Hit key to terminate...");
            Console.ReadLine(); 
        }

        // Method to show current time... 
        public static void ShowTime(object state)
        {
        Console.WriteLine("From the thread #{0}, it is background?{1}: time is {2}, param is {3}", 
            Thread.CurrentThread.ManagedThreadId, 
            Thread.CurrentThread.IsBackground,
            DateTime.Now.ToLongTimeString(), 
            state.ToString()); 
        }
    }
 } 

产生以下输出

  

*****定时器应用程序*****

     

在主题#1中   点击键终止...
  从线程#4开始,它是背景?真:时间是10:37:54 PM,param是来自Main()的Hello   从线程#4开始,它是背景?真:时间是10:37:55 PM,param是来自Main()的Hello   从线程#5开始,它是背景?真:时间是10:37:56 PM,param是来自Main()的Hello   从线程#4开始,它是背景?真:时间是10:37:57 PM,param是来自Main()的Hello   从线程#5开始,它是背景?真:时间是10:37:58 PM,param是来自Main()的Hello   从线程#4开始,它是背景?真:时间是10:37:59 PM,param是来自Main()的Hello   从线程#5开始,它是背景?真:时间是10:38:00 PM,param是来自Main()的Hello   ...
  按任意键继续 。 。

System.Threading.Timer是否一次使用多个线程进行回调?

1 个答案:

答案 0 :(得分:1)

它使用线程池,使用它在每个时间间隔找到的第一个线程。计时器只是触发这些线程的触发。

void Main()
{
    System.Threading.Timer timer = new Timer((x) =>
    {
        Console.WriteLine($"{DateTime.Now.TimeOfDay} - Is Thread Pool Thread: {Thread.CurrentThread.IsThreadPoolThread} - Managed Thread Id: {Thread.CurrentThread.ManagedThreadId}");
        Thread.Sleep(5000);

    }, null, 1000, 1000);

    Console.ReadLine();
}

输出

07:19:44.2628607 - Is Thread Pool Thread: True - Managed Thread Id: 10
07:19:45.2639080 - Is Thread Pool Thread: True - Managed Thread Id: 13
07:19:46.2644998 - Is Thread Pool Thread: True - Managed Thread Id: 9
07:19:47.2649563 - Is Thread Pool Thread: True - Managed Thread Id: 8
07:19:48.2660500 - Is Thread Pool Thread: True - Managed Thread Id: 12
07:19:49.2664012 - Is Thread Pool Thread: True - Managed Thread Id: 14
07:19:50.2669635 - Is Thread Pool Thread: True - Managed Thread Id: 15
07:19:51.2679269 - Is Thread Pool Thread: True - Managed Thread Id: 10
07:19:52.2684307 - Is Thread Pool Thread: True - Managed Thread Id: 9
07:19:53.2693090 - Is Thread Pool Thread: True - Managed Thread Id: 13
07:19:54.2839838 - Is Thread Pool Thread: True - Managed Thread Id: 8
07:19:55.2844800 - Is Thread Pool Thread: True - Managed Thread Id: 12
07:19:56.2854568 - Is Thread Pool Thread: True - Managed Thread Id: 15

在上面的代码中,我们将线程设置为等待5秒,因此在打印到控制台之后,线程将保持活动状态5秒钟,然后再完成执行并返回到线程池。

计时器每秒都会继续触发,不管它是否等待它触发完成的线程。