Search code examples
c#timertry-catch-finallythreadabortexception

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?


Solution

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