日期:2014-05-17  浏览次数:20819 次

System.Threading.Timer 的问题 在线等。。。
System.Threading.Timer 
假如说这个timer每200ms执行一次A
那么timer启动后是每200ms申请一个新线程执行A
  还是就是在启动的时候申请一个新线程,然后以后的A都在这个线程里执行???




------解决方案--------------------
首先看如下程序
C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace ConsoleApplication1
{
    class Program
    {
        static void callback(Object param)
        {
            string id = Guid.NewGuid().ToString(); //放一个随机产生的id用来区分每个callback调用,为了让输出看得更清楚
            for (int i = 0; i < 5; i++)
            {
                Console.WriteLine("{0} loop {1}", id, i);
                System.Threading.Thread.Sleep(1000);
            }
        }
        static void Main(string[] args)
        {
            System.Threading.Timer t = new System.Threading.Timer(callback, null, 0, 1000);
            Console.Read();
        }
    }
}

------解决方案--------------------
这是程序开始运行的一个输出片段:
c86e4a56-5f4d-4be9-9752-9af0a1387529 loop 0
c86e4a56-5f4d-4be9-9752-9af0a1387529 loop 1
d4e6da32-5254-4bb9-9a11-a1cf25863aa7 loop 0
c86e4a56-5f4d-4be9-9752-9af0a1387529 loop 2
d4e6da32-5254-4bb9-9a11-a1cf25863aa7 loop 1
ab622f0b-7f8d-40b1-b74e-1722e51eb86a loop 0
c86e4a56-5f4d-4be9-9752-9af0a1387529 loop 3
4edd3e9d-1c2b-4e16-be09-4fefc38c230c loop 0
d4e6da32-5254-4bb9-9a11-a1cf25863aa7 loop 2
ab622f0b-7f8d-40b1-b74e-1722e51eb86a loop 1
c86e4a56-5f4d-4be9-9752-9af0a1387529 loop 4
4edd3e9d-1c2b-4e16-be09-4fefc38c230c loop 1
47ac0157-43a4-4224-b4b9-770721ebbb57 loop 0
d4e6da32-5254-4bb9-9a11-a1cf25863aa7 loop 3
ab622f0b-7f8d-40b1-b74e-1722e51eb86a loop 2
5b7f4baa-858d-430e-8ccf-bb0ee90de88e loop 0
4edd3e9d-1c2b-4e16-be09-4fefc38c230c loop 2
47ac0157-43a4-4224-b4b9-770721ebbb57 loop 1
35c7f4a4-a528-47d8-b0b9-5cc1d25c1089 loop 0
d4e6da32-5254-4bb9-9a11-a1cf25863aa7 loop 4
ab622f0b-7f8d-40b1-b74e-1722e51eb86a loop 3
5b7f4baa-858d-430e-8ccf-bb0ee90de88e loop 1
4edd3e9d-1c2b-4e16-be09-4fefc38c230c loop 3
47ac0157-43a4-4224-b4b9-770721ebbb57 loop 2
35c7f4a4-a528-47d8-b0b9-5cc1d25c1089 loop 1
5dbb47cf-3444-47bf-8c4c-9b5ea2a22941 loop 0
ab622f0b-7f8d-40b1-b74e-1722e51eb86a loop 4
5b7f4baa-858d-430e-8ccf-bb0ee90de88e loop 2
4edd3e9d-1c2b-4e16-be09-4fefc38c230c loop 4
47ac0157-43a4-4224-b4b9-770721ebbb57 loop 3
35c7f4a4-a528-47d8-b0b9-5cc1d25c1089 loop 2
5dbb47cf-3444-47bf-8c4c-9b5ea2a22941 loop 1
e143eda8-0790-49cb-a685-b9c2dab7573b loop 0
5b7f4baa-858d-430e-8ccf-bb0ee90de88e loop 3
70adbb9b-90c2-4924-90e9-31d53441efa1 loop 0
a42a5128-dd26-48fb-9d53-3dc2996dcad1 loop 0
35c7f4a4-a528-47d8-b0b9-5cc1d25c1089 loop 3
e143eda8-0790-49cb-a685-b9c2dab7573b loop 1
5b7f4baa-858d-430e-8ccf-bb0ee90de88e loop 4
47ac0157-43a4-4224-b4b9-770721ebbb57 loop 4
70adbb9b-90c2-4924-90e9-31d53441efa1 loop 1
5dbb47cf-3444-47bf-8c4c-9b5ea2a22941 loop 2
9cf82d63-25d9-441d-adee-828ad1874b5c loop 0
a42a5128-dd26-48fb-9d53-3dc2996dcad1 loop 1
35c7f4a4-a528-47d8-b0b9-5cc1d25c1089 loop 4
e143eda8-0790-49cb-a685-b9c2dab7573b loop 2
70adbb9b-90c2-4924-90e9-31d53441efa1 loop 2
5dbb47cf-3444-47bf-8c4c-9b5ea2a22941 loop 3
5edc9ca3-2715-48d4-bfb6-c90341b6a84c loop 0
9cf82d63-25d9-441d-adee-828ad1874b5c loop 1
a42a5128-dd26-48fb-9d53-3dc2996dcad1 loop 2
e143eda8-0790-49cb-a685-b9c2dab7573b loop 3
70adbb9b-90c2-4924-90e9-31d53441efa1 loop 3
5dbb47cf-3444-47bf-8c4c-9b5ea2a22941 loop 4
ada6dc4b-7774-4cd1-a202-3ed89a25955a loop 0
5edc9ca3-2715-48d4-bfb6-c90341b6a84c loop 1
9cf82d63-25d9-441d-adee-828ad1874b5c loop 2
a42a5128-dd26-48fb-9d53-3dc2996dcad1 loop 3
e143eda8-0790-49cb-a685-b9c2dab7573b loop 4
70adbb9b-90c2-4924-90e9-3