Does ThreadAbortException still enforce executing the code in finally (try/catch) section?
Asked Answered
T

5

6

I have a System.Timers.Timer timer which it's AutoReset is set to false. I use a try/finally to insure I Start the timer at the end of it's callback (I use the timer this way to prevent overlapping of callback execution). Code:

// inside timer call back
try
{
    // Do something
}
finally
{
    timer.Start(); // Is this line always executed?
}

My question is what happens if the executing thread is Aborted? Does the finally section still executed or there's no thread to run that part?

Trentontrepan answered 23/5, 2011 at 15:29 Comment(1)
As a side-note, if you're in the timer callback, you're on a thread-pool thread - and thread-pool threads are never aborted.Riggs
C
4

Yes, that line will always be executed and the abort blocked until the code in the finally clause finishes.

Consociate answered 23/5, 2011 at 15:32 Comment(0)
D
7

The official source...

When a call is made to the Abort method to destroy a thread, the common language runtime throws a ThreadAbortException. ThreadAbortException is a special exception that can be caught, but it will automatically be raised again at the end of the catch block. When this exception is raised, the runtime executes all the finally blocks before ending the thread. Because the thread can do an unbounded computation in the finally blocks or call Thread.ResetAbort to cancel the abort, there is no guarantee that the thread will ever end. If you want to wait until the aborted thread has ended, you can call the Thread.Join method. Join is a blocking call that does not return until the thread actually stops executing.

Read more about it on MSDN.

Decelerate answered 23/5, 2011 at 15:33 Comment(0)
C
4

Yes, that line will always be executed and the abort blocked until the code in the finally clause finishes.

Consociate answered 23/5, 2011 at 15:32 Comment(0)
G
4

Quoth the documentation (empahsis mine):

When this method is invoked on a thread, the system throws a ThreadAbortException in the thread to abort it. ThreadAbortException is a special exception that can be caught by application code, but is re-thrown at the end of the catch block unless ResetAbort is called. ResetAbort cancels the request to abort, and prevents the ThreadAbortException from terminating the thread. Unexecuted finally blocks are executed before the thread is aborted.

The thread is not guaranteed to abort immediately, or at all. This situation can occur if a thread does an unbounded amount of computation in the finally blocks that are called as part of the abort procedure, thereby indefinitely delaying the abort. To wait until a thread has aborted, you can call the Join method on the thread after calling the Abort method, but there is no guarantee that the wait will end.

So the answer is yes, the finally blocks will be executed.

Gallican answered 23/5, 2011 at 15:33 Comment(0)
I
1

Yes the finally will always be used no matter how it exits from try,

 Whereas catch is used to handle exceptions that occur in a statement block, 
 finally is used to guarantee a statement block of code executes regardless 
 of how the preceding try block is exited.

Read more it on MSDN.

Introvert answered 23/5, 2011 at 16:28 Comment(0)
S
1

if the thread has already been aborted, the catch block and the finally block can continue to execute.

Please refer this link to get a clear picture on how it is handled in system.threading class Plumbing the Depths of the ThreadAbortException

Shoon answered 22/6, 2013 at 19:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.