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

Wade Maxfield wmaxfield at gmail.com
Tue Apr 4 13:50:00 EDT 2006

Hi Felipe,

  First, thank you.

  Second, yes, I did use a variable to do what you stated (it was in the
code fragments).  When I found the abort() did not work all the time, I set
a variable to true, which caused my thread to abort.

  Now, in regards to programming practices.  I believe you are correct.  I
agree in principal with everything you state.  I typically practice those
things, as unbelievable as that might sound.  I'm more into systems now, but
I've written real time sw for years.

  However, if you look at Microsoft's .NET programming in C# examples and
tutorials, their C# tutorial (see Microsoft .NET SDK, C# Programming
Reference, "Threading Tutorial") on threads shows you to

  1) Spin while thread is not alive
  2) call abort for thread to be closed

   I agree that a sleep() is a good thing while waiting for thread to start
running.  However, I thought one of the points of "managed" code was to
allow this sort of thing, not necessarily paying attention to cpu loading
issues, as well as memory issues?  That is why I swallowed MS's tutorial
hook line and sinker.

  In any case, regardless of the efficacy of the program I've written, the
point that mono developers must ponder is that other coders following my
footsteps will code exactly the same way, the way that Microsoft Tutorial
states it should.  When it fails to abort under mono, but always aborts
under .NET, they will claim it is a bug in Mono.

  It does appear I overlooked a join() call, which forces the first thread
to deadlock until the abort happens.  However, I never had the problem of a
failing abort in .NET.  In this case, I could care less if the thread
aborted a few hundred milliseconds to one second later than the main thread.

  As I indicated earlier, I set a variable which caused my routine to bail,
causing the thread to naturally abort (similar to your proposal, but more
crude).  This method will work in all versions, .NET and Mono.

   I was wondering if perhaps the abort() routine in Mono might need
tweaking. I was assuming that in most cases, (except for System.Managed and
other issues of similar ilk) that Mono would typically want to work the same
way as .NET.

  I think Mono is great!  I'm very, very happy to see it.  The work thus far
has been astounding and exciting.   If the way abort() works is the one of
the few differences, I don't have any problems using Mono.  It would need to
be documented, however, for future folks blindly following MS

  BTW, the original .NET code ran in Mono without recompile, except for one
small issue.  I'm tracking down some other issues now.

thanks again,

On 4/4/06, Felipe Almeida Lessa <felipe.lessa at gmail.com> wrote:
> 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
> possible.
> The relevant abortion method:
> public void Abort() {
>         this.aborted = true;
>         try {
>                 if (this.abortable) {
>                         this.webRequest.Abort();
>                         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.
> HTH,
> --
> Felipe.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.ximian.com/pipermail/mono-devel-list/attachments/20060404/4a9cc005/attachment.html 

More information about the Mono-devel-list mailing list