In my C# program, I have a thread that represents a running test, which can be aborted by a click on a button labeled "Interrupt execution". In order for the thread (and therefore the test) to terminate in an elegant manner (and do some vital work beforehand), this button is enabled only in some well-defined moments, in which I catch ThreadAbortedException
, do Thread.ResetAbort()
and die beautifully (that is, the thread).
The problem is that, in the time window in which aborting is possible, there are some tasks that need to be done from start to finish once initiated, and, so, I fear TAE. Locks don't provide a solution for this, and, although finally blocks do, I don't find it elegant to wrap important code in the following manner:
try {
} finally {
// vital code
}
However, I didn't find any other solution.
Is this another way to delay the interference of ThreadAbortException
until the end of the block?