title: Interrupt a sleeping Thread
body: <p>Is there a way to Interupt a sleeping thread?  If I have code similar to this.</p>
  
  <pre><code>while(true){
      if(DateTime.Now.Subtract(_lastExecuteTime).TotalHours &gt; 1){
          DoWork();
          _lastExecuteTime = DateTime.Now();
          continue; 
      }
      Thread.Sleep(10000) //Sleep 10 seconds
      if(somethingIndicatingQuit){
          break;
      }
  
  }
  </code></pre>
  
  <p>I'm wanting to execute DoWork() every hour. So, I'd like to sleep a little longer then 10 seconds. Say check every 10 minutes or so.  However, if set my sleep to 10 minutes, and I want to kill this background task, I have to wait for the sleep to resume.</p>
  
  <p>My actual code is using a Threading.ManualResetEvent to shut down the background work, but my issue is with the ThreadSleep code. I can post more code if necessary.</p>
  
  <p>OK, I'm going to add a bit more complete code here as I think it will answer some of the questions.</p>
  
  <pre><code>private readonly ManualResetEvent _shutdownEvent = new ManualResetEvent(false);
  private readonly ManualResetEvent _pauseEvent = new ManualResetEvent(true);
  private Thread _backGroundWorkerThread;
  
  //This starts our work
  public void Start() {
      _backGroundWorkerThread = new Thread(ExecuteWorker) {IsBackground = true, Name = WorkerName + "_Thread"};
      _shutdownEvent.Reset();
      _backGroundWorkerThread.Start();
  }
  internal void Stop() {
      //Signal the shutdown event
      _shutdownEvent.Set();
  
      //Make sure to resume any paused threads
      _pauseEvent.Set();
  
      //Wait for the thread to exit
      _backGroundWorkerThread.Join();
  
  }
  
  private void ExecuteWorker() {
      while (true) {
          _pauseEvent.WaitOne(Timeout.Infinite);
  
          //This kills our process
          if (_shutdownEvent.WaitOne(0)) {
             break;
          }
  
          if (!_worker.IsReadyToExecute) {
              //sleep 5 seconds before checking again. If we go any longer we keep our service from shutting down when it needs to.
              Thread.Sleep(5000);
              continue;
          }
          DoWork();
  
      }
  }
  </code></pre>
  
  <p>My problem is here, </p>
  
  <pre><code>_backGroundWorkerThread.Join();
  </code></pre>
  
  <p>This waits for the Thread.Sleep within the ExecuteWorker() that is running in my background thread.  </p>