Re: How do I gently terminate a thread???

From: TT \(Tom Tempelaere\) (_N_0SPA|/\|titi_____at_hotmail.com|/\|APS0_N_)
Date: 05/07/04


Date: Fri, 07 May 2004 19:08:08 GMT

Hi,

"Ryan Gregg" <rgregg@wheatlandsystems.com> wrote in message
news:%23gZ3MCFNEHA.1644@TK2MSFTNGP09.phx.gbl...
> Say for instance I have a thread that I fire off that's running some loop
of
> code that's monitoring status or something. I know I could define a
> variable in a shared address space, and have that loop until the variable
> goes false or something, which would then have the thread exit. Is there
a
> way though without using this method to cause a thread to shut down
without
> calling Abort() ?

You could throw an exception that is only caught in the main method of the
your thread, then just return.

Or, if you wait on WaitHandle-s, you join with other threads or you sleep a
lot (Thread.Sleep), you could call Thread.Interrupt. The next wait, join or
sleep on the thread will then throw a ThreadInterruptedException. Beware of
general exception handlers that could catch this exception and ignore it.

The nice thing about Thread.Abort is that it throws a
ThreadAbortedException, which is a special kind of exception. It will be
rethrown when caught until the thread exits, or until you call
Thread.ResetAbort.

Cheers,

---
Tom Tempelaere.


Relevant Pages

  • Re: determine if file is being used by another process
    ... > never be predicated on exception handling, especially when there is a way ... between - see later] and only terminate the loop when the ... API like CreateFile is a closed loop without some sleep in between the ... such a way that it can dramatically increase the file creation time when run ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: How do I gently terminate a thread???
    ... > Say for instance I have a thread that I fire off that's running some loop ... which would then have the thread exit. ... You could throw an exception that is only caught in the main method of the ... sleep on the thread will then throw a ThreadInterruptedException. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: long double versions of functions in gcc under Cygwin
    ... rather than the nearest enclosing one) and a decent exception ... them it doesn't seem like goto usage would be affected ... int typfun() ... Why use a for loop when it is just a while loop in disguise? ...
    (comp.lang.c)
  • Re: CInternetSession
    ... the presence of the Sleepindicates the serious design flaw. ... Sleep() calls around like pixie dust, your design is fundamentally broken and will need to ... If you use Sleepin a loop, your design is probably wrong and needs to be ... The "First Chance Exception" message usually indicates nothing harmful. ...
    (microsoft.public.vc.mfc)
  • Re: Get_Line problem (GNAT bug?)
    ... Now, if the program specs says: "read the lines from input until EOF", then this for me immediately translates into a loop with some exit condition. ... "Read until" - you have a regular end-of-sequence condition here. ... I'm not convinced that exception might be a correct design choice for breaking the loop that reads data from well formatted file. ...
    (comp.lang.ada)