简体   繁体   English

Thread.ResetAbort 的好处

[英]Benefits of Thread.ResetAbort

When a thread is canceled via Thread.Abort() , a ThreadAbortException is thrown inside the Thread on which Thread.Abort() was called on.当通过Thread.Abort()取消线程时,会在调用Thread.Abort()的线程内抛出 ThreadAbortException。 This leads the thread to immediately stop its work and the exception bubbles up the call stack until it leaves the thread's main method.这会导致线程立即停止工作,异常会在调用堆栈中冒泡,直到它离开线程的 main 方法。 This causes the thread to be aborted.这会导致线程中止。

What are the benefits of an ExceptionHandler for the ThreadAbortException in the threads main method where Thread.ResetAbort() is called, when the thread terminates itself anyway after the catch block due to stepping out its main method?Thread.ResetAbort()的线程主方法中的 ThreadAbortException 的 ExceptionHandler 有什么好处,当线程在 catch 块之后由于退出其主方法而终止自身时?

private void ThreadMainMethod( )
{
    try
    {
        while(runningAllowed = true)
        {
            //Do some work here
        }
    }
    catch ( ThreadAbortException )
    {
        Thread.ResetAbort( );
    }
}

One scenario I can think of is that you want to take down the thread in a controlled manner.我能想到的一种情况是您希望以受控方式删除线程。 Let's say you have a worker thread that is polling some resource.假设您有一个正在轮询某些资源的工作线程。 If the application's main thread invokes Abort on the worker thread, a ThreadAbortException is thrown.如果应用程序的主线程在工作线程上调用Abort ,则会抛出ThreadAbortException You can then catch that exception in start method for the worker thread, call ResetAbort and then finish the method by releasing resource, closing open files/connections and so on:然后,您可以在工作线程的 start 方法中捕获该异常,调用ResetAbort ,然后通过释放资源、关闭打开的文件/连接等来完成该方法:

public void ThreadStarter()
{
    try
    {
        RunWorkerLoop();
    }
    catch (ThreadAbortException)
    {
        Thread.ResetAbort();
    }

    // clean up and end gracefully

}

Probably the only reason you'd do that would be if you were in a great position to decide whether or not you should actually abort.可能你这样做的唯一原因是,如果你有能力决定是否真的应该流产。

So perhaps the thread would catch it, check the status of something, and then go back about its work again.所以也许线程会捕获它,检查某些东西的状态,然后再次返回它的工作。 Though this does imply that you're basically using the ' .abort() ' to control the flow of this thread.尽管这确实意味着您基本上是在使用“ .abort() ”来控制该线程的流程。 And that's quite a bad idea.这是一个非常糟糕的主意。 You should communicate with it in another way.你应该用另一种方式与它交流。

In general, I would think there are not many cases where this is a good idea, and it wouldn't be the advice for any particular pattern or implementation I can think of.总的来说,我认为这是一个好主意的情况并不多,而且它不会成为我能想到的任何特定模式或实现的建议。

In you particular case it doesn't really make a difference, because the thread will be terminated once the method is done running.在您的特定情况下,它并没有真正产生影响,因为一旦方法运行完毕,线程就会终止。

However, in other case you may have a method that runs in an endless loop.但是,在其他情况下,您可能有一个无限循环运行的方法。 In this case, you can shutdown the thread using the ThreadAbortException (I am not saying that you should, but you could).在这种情况下,您可以使用 ThreadAbortException 关闭线程(我不是说您应该,但您可以)。 If the thread for some reason determines to continue despite the exception it needs to call ResetAbort to prevent the runtime to automatically rethrow the ThreadAbortException.如果线程由于某种原因决定在异常情况下继续运行,它需要调用 ResetAbort 以防止运行时自动重新抛出 ThreadAbortException。

我发现调用ResetAbort()对使用超时模式的WaitFor 的这种优雅实现有很大帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM