Xaqron
Xaqron

Reputation: 30857

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

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?

Upvotes: 6

Views: 1429

Answers (5)

Sunil
Sunil

Reputation: 2905

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

Upvotes: 1

Gonzalo
Gonzalo

Reputation: 21175

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

Upvotes: 4

Chris Baxter
Chris Baxter

Reputation: 16353

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.

Upvotes: 7

Qwan
Qwan

Reputation: 157

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.

Upvotes: 1

David Heffernan
David Heffernan

Reputation: 612993

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.

Upvotes: 4

Related Questions