Thread.Interrupt not working

Why is my Thread.Interrupt not working?

Interrupt code:

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 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 went through the code. I see that the interrupt is being called (Sleep or wait are not active commands at that time), and I can see that the sleep is being called, but the interrupt error is not interrupted (neither in sleep, nor on WaitOne, even when the thread blocks on WaitOne )

What am I doing wrong?

Note: .Net 2.0

+3
source share
1 answer

... , , Interrupt . / Monitor.Wait/Pulse, , . , .

+3

Source: https://habr.com/ru/post/1718409/


All Articles