[Mono-dev] Thread Abort() sometimes fails in mono

Felipe Almeida Lessa felipe.lessa at gmail.com
Tue Apr 4 12:13:10 EDT 2006

Em Ter, 2006-04-04 às 08:26 -0600, Wade Maxfield escreveu:
> Hi,

Hi! Correct me if I'm wrong but...

> /*doesn't always work ---->*/    Global.WebThread.Abort();

You shouldn't be using Thread.Abort, right? AFAIK, this is a bad way of
killing threads.

>         while (!Global.WebThread.IsAlive);

Why do you make the CPU go to 100% here? At least some Sleep should be
better, no?


I didn't read all of your code, but I suspect you should be using
another thread abortion method. I have some code that uses a thread with
some web requests, and to abort it I do 2 things:

1) Create a variable that is marked false when I want the thread to die.
The thread's code periodically checks for the variable.
2) Save the WebRequest on a private variable to try to abort it, if

The relevant abortion method:

public void Abort() {
	this.aborted = true;
	try {
		if (this.abortable) {
			Logger.Log(this, "WebRequest aborted with (apparent) success!");
		} else
			Logger.Log(this, "WebRequest is not abortable.");
	} catch (Exception e) {
		Logger.Log(this, "Failed to abort the WebRequest:\n" + e.ToString());

The method name is "Abort" because this class does not derive from
Thread. Also, you can safely ignore all Logger::Log calls.

abortable is a variable that is set to false when the code starts an
operation on the WebRequest that would probably crash the program if it
was aborted.

It's also worth noting that we just try to abort the WebRequest and
ignore any exceptions -- the code will stop anyway sometime soon as we
set the aborted variable.



More information about the Mono-devel-list mailing list