4.14. Terminate the Execution of a Thread

Problem

You need to terminate an executing thread without waiting for it to finish on its own accord.

Solution

Call the Abort method of the Thread object you want to terminate.

How It Works

It is better to write your code so that you can signal to a thread that it should shut down and allow it to terminate naturally. Recipes 4-8, 4-9, and 4-10 demonstrate this technique (using a Boolean flag). However, sometimes you will want a more direct method of terminating an active thread.

Calling Abort on an active Thread object terminates the thread by throwing a System.Threading. ThreadAbortException in the code that the thread is running. You can pass an object as an argument to the Abort method, which is accessible ...

Get Visual Basic 2008 Recipes: A Problem-Solution Approach now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.