System.Threading.Timer的Dispose方法不适用于ManualResetEventSlim?

时间:2010-10-08 03:35:17

标签: c# windows-services timer manualresetevent

我有一个示例控制台应用程序的以下代码来模拟Windows服务。

class Program
{
    private Timer timer;
    private object syncRoot = new object();
    private bool stopSignalled = false;
    private ManualResetEventSlim mre = new ManualResetEventSlim(false);
    static void Main(string[] args)
    {
        Program p = new Program();
        p.Start();
        Console.ReadLine();
        p.Stop();
        Console.WriteLine("Stopped at:{0:G}", DateTime.Now);
    }

    private void Stop()
    {
        stopSignalled = true;
        mre.Wait();            

    }
    private void Timercallback(object state)
    {
        lock (syncRoot)
        {
            if (!stopSignalled)
            {
                 Console.WriteLine("Callback invoked  at:{0:G}",DateTime.Now);

            }
            else
            {
                timer.Dispose(mre.WaitHandle);
                Console.WriteLine("Timer disposed at:{0:G}", DateTime.Now);
            }
        }

    }

    private void Start()
    {
        Console.WriteLine("Started at:{0:G}",DateTime.Now);
        timer = new Timer(Timercallback,null,TimeSpan.Zero,TimeSpan.FromSeconds(1));

    }
}

我很惊讶接受WaitHandle的Dispose重载在使用ManualResetEventSlim时从不发出信号。 如果我改变代码以使用ManualResetEvent而不是这样,那么句柄确实已经发出信号。

    class Program
{
    private Timer timer;
    private object syncRoot = new object();
    private bool stopSignalled = false;
    private ManualResetEvent mre = new ManualResetEvent(false);
    static void Main(string[] args)
    {
        Program p = new Program();
        p.Start();
        Console.ReadLine();
        p.Stop();
        Console.WriteLine("Stopped at:{0:G}", DateTime.Now);
    }

    private void Stop()
    {
        stopSignalled = true;
        mre.WaitOne();            

    }
    private void Timercallback(object state)
    {
        lock (syncRoot)
        {
            if (!stopSignalled)
            {
                 Console.WriteLine("Callback invoked  at:{0:G}",DateTime.Now);

            }
            else
            {
                timer.Dispose(mre);
                Console.WriteLine("Timer disposed at:{0:G}", DateTime.Now);
            }
        }

    }

    private void Start()
    {
        Console.WriteLine("Started at:{0:G}",DateTime.Now);
        timer = new Timer(Timercallback,null,TimeSpan.Zero,TimeSpan.FromSeconds(1));

    }
}

什么给了!

0 个答案:

没有答案