Преимущества потока. ResetAbort

Очень конкретная Подрывная деятельность, но это дает достойное понимание основ управления версиями.

http://svnbook.red-bean.com/

6
задан PVitt 27 August 2009 в 07:39
поделиться

3 ответа

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. 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.

3
ответ дан 8 December 2019 в 16:06
поделиться

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. 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:

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

    // clean up and end gracefully

}
7
ответ дан 8 December 2019 в 16:06
поделиться

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). 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.

3
ответ дан 8 December 2019 в 16:06
поделиться
Другие вопросы по тегам:

Похожие вопросы: