Posted by: Cirilo Meggiolaro | 10/19/2008

Tip of the day #5 – Stopping threads safely

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…


Enjoy it.



  1. Nice tip! Avoiding problems hâ!

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s


%d bloggers like this: