How can I detect a ThreadAbortException in a finally block? (.NET)

前端 未结 7 2172
太阳男子
太阳男子 2020-12-15 12:08

I have some critical logic in a finally block (with an empty try block), because I want to guarantee that the code gets executed even if the thread is aborted. However, I\'d

相关标签:
7条回答
  • 2020-12-15 12:31

    Have you tried something like this?

    try {
        try { }
        catch (ThreadAbortException)
        {
          ThreadAbortExceptionBool = true;
        }
        finally {
            // critical logic
            if (ThreadAbortExceptionBool)
              // Whatever
        }
    } 
    catch(Exception ex) {
        // ThreadAbortException is not caught here, but exceptions thrown
        // from within the critical logic are
    }
    
    0 讨论(0)
提交回复
热议问题