When working with multi-thread code we often face the need of cancel the execution of a specific thread.
Although it is simple to request the cancel by invoking the Thread.Abort() method (that throws a ThreadAbortException) we need to assure that critical code has run properly and data inconsistency or process flaws will not be resulting of this cancel.
In .NET we can define a region where the thread will keep running even if a Thread.Abort() has been requested. After the code into the region has run the exception will be thrown.
To define a critical region, just add the following begin / end statement calls:
// Critical code…