3

Why is my Thread.Interrupt not working?

The code doing the interrupt:

public void Stop()
{
    const string LOG_SOURCE = "ProcessingDriver";

    if (_Enabled)
    {
        try
        {
            RequestProcessor.Disable();
            if (ProcessingThread != null)
            {
                ProcessingThread.Interrupt();
                ProcessingThread.Join();
            }
        }
        catch (Exception ex)
        {
            WriteLog(LOG_SOURCE, ex);
        }
    }
}

The code that I expect to stop:

private void ProcessRequests()
{
    const string LOG_SOURCE = "ProcessRequests";
    try
    {
        ProcessingThread = Thread.CurrentThread;
        while (!_Disposed)
        {
            _ProcessingWaitHandle.WaitOne();
            int count = GetRequestCount();
            while (count > 0)
            {
                try
                {
                    ExportRequest er = GetRequest();
                    ProcessRequest(er);
                }
                catch (ThreadInterruptedException)
                {
                    throw;
                }
                catch (Exception ex)
                {
                    WriteLog(LOG_SOURCE,
                         ex);
                    WriteLog(LOG_SOURCE,
                        "Request Failed.");
                }
                //Suspend to catch interupt
               Thread.Sleep(1);
                count = GetRequestCount();
            }
        }
    }
    catch (ThreadInterruptedException)
    {
        WriteLog(LOG_SOURCE,
            "Interupted. Exiting.", LogType.Info);
    }
    catch (Exception critEx)
    {
        //Should never get here
        WriteLog(LOG_SOURCE, critEx);
        WriteLog(LOG_SOURCE,
            "Serious unhandled error.  Please restart.", LogType.Critical);
    }
}

I have stepped through the code. I can see Interrupt being called (Sleep or wait are not the active commands at the time), and I can see sleep being called, but no Interrupt error is ever thrown (neither on the sleep, or on the WaitOne, even when the thread blocks on WaitOne).

What am I doing wrong?

Note: .Net 2.0

1
  • +1 for clarity and disclosure.
    – Ben S
    Sep 23, 2009 at 19:12

1 Answer 1

3

Hmmm... it does look like it should work, but I would advise you not to use Interrupt in the first place. Use events and/or Monitor.Wait/Pulse to tell the thread that you want to stop. That's a generally simpler approach, and one that gives the worker thread more control over stopping in an orderly manner.

6
  • I like the Interrupt method because it would automatically kick out of my _ProcessingWaitHandle.WaitOne();
    – C. Ross
    Sep 23, 2009 at 19:23
  • 2
    Jon is right. Here is why: bluebytesoftware.com/blog/… Sep 23, 2009 at 19:27
  • C. Ross: The point of a wait handle is that you can Set it without just interrupting the thread...
    – Jon Skeet
    Sep 23, 2009 at 19:28
  • @Jon: But since I'm already blocking on one wait handle, I'd have to come out of that one to check my new "TimeToQuit" wait handle. Esentially I'd have to poll between my two handles.
    – C. Ross
    Sep 23, 2009 at 19:37
  • 2
    @C. Ross: What about WaitHandle.WaitAny() ? Sep 23, 2009 at 19:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.