MaualResetEventSlim没有停止线程

时间:2014-01-24 11:26:04

标签: c# .net multithreading manualresetevent

我的示例应用程序中有以下代码:

private static ManualResetEventSlim _screenSaverOn = new ManualResetEventSlim();
private static ManualResetEventSlim _screenSaverOff = new ManualResetEventSlim();

在类的构造函数中,我初始化了两个线程:

Thread threadOn = new Thread(CheckIfScreenSaverIsOn) {IsBackground = true};
Thread threadOff = new Thread(CheckIfScreenSaverIsOff) {IsBackground = true};

threadOn.Start();
threadOff.Start();

这些线程执行的方法是:

private static void CheckIfScreenSaverIsOn()
{
    log.Info("Thread1 - started running");
    while (true)
    {
        if (NativeMethods.IsScreensaverRunning())
        {
            log.Info("Thread1 - Screen Saver Started Running");
            _screenSaverOff.Set();
            log.Info("Thread1 - Waiting");
            _screenSaverOn.Wait();
            log.Info("Thread1 - Started Again");
        }

        Thread.Sleep(2000);
    }
}

private static void CheckIfScreenSaverIsOff()
{
    log.Info("Thread2 - Started Running");
    _screenSaverOff.Wait();

    while (true)
    {
        // returns true/false
        if (!NativeMethods.IsScreensaverRunning())
        {
            log.Info("Thread2 - Screen Saver Stopped Running");
            _screenSaverOn.Set();
            log.Info("Thread2 - Waiting");
            _screenSaverOff.Wait();
            log.Info("Thread2 - Started Again");
        }

        Thread.Sleep(2000);
    }
}

我面临的问题是,即使在调用_screenSaverOff.Wait()方法之后,“thread2”也没有被阻止。日志条目如下:

2014-01-24 16:43:16,087 [4] INFO  SystemEvent.TimeTracker Thread1 - started running
2014-01-24 16:43:16,088 [5] INFO  SystemEvent.TimeTracker Thread2 - Started Running
2014-01-24 16:46:16,129 [4] INFO  SystemEvent.TimeTracker Thread1 - Screen Saver Started Running
2014-01-24 16:46:16,130 [4] DEBUG SystemEvent.TimeTracker Original Start Time : 01/24/2014 16:43:16
2014-01-24 16:46:16,131 [4] DEBUG SystemEvent.TimeTracker Current Time : 01/24/2014 16:46:16
2014-01-24 16:46:16,131 [4] DEBUG SystemEvent.TimeTracker Total Active Minute : 3
2014-01-24 16:46:16,188 [4] INFO  SystemEvent.TimeTracker Thread1 - Waiting
2014-01-24 16:46:26,189 [5] INFO  SystemEvent.TimeTracker Thread2 - Screen Saver Stopped Running
2014-01-24 16:46:26,189 [4] INFO  SystemEvent.TimeTracker Thread1 - Started Again
2014-01-24 16:46:26,189 [5] INFO  SystemEvent.TimeTracker Thread2 - Waiting
2014-01-24 16:46:26,189 [5] INFO  SystemEvent.TimeTracker Thread2 - Started Again
2014-01-24 16:46:28,191 [5] INFO  SystemEvent.TimeTracker Thread2 - Screen Saver Stopped Running
2014-01-24 16:46:28,191 [5] INFO  SystemEvent.TimeTracker Thread2 - Waiting
2014-01-24 16:46:28,191 [5] INFO  SystemEvent.TimeTracker Thread2 - Started Again
2014-01-24 16:46:30,191 [5] INFO  SystemEvent.TimeTracker Thread2 - Screen Saver Stopped Running
2014-01-24 16:46:30,191 [5] INFO  SystemEvent.TimeTracker Thread2 - Waiting
2014-01-24 16:46:30,191 [5] INFO  SystemEvent.TimeTracker Thread2 - Started Again
2014-01-24 16:46:32,192 [5] INFO  SystemEvent.TimeTracker Thread2 - Screen Saver Stopped Running

由于 Pawan Mishra

1 个答案:

答案 0 :(得分:2)

您正在使用手动重置事件,但您从未重置它们。您需要拨打Reset,否则他们会在您设置后始终发出信号。