Home All Groups Group Topic Archive Search About

Aborting a thread while on sleep

Author
31 Jan 2006 4:21 PM
barbutz
Hi,

I have a thread which runs in a loop. Between each loop cycle it sleeps for
10 seconds.
The problem is that if i'm aborting the thread during its sleep, the thread
will be aborted only after the sleep time is over meaning it can be up to 10
seconds.

Is there a way to force the abortion immediately without waiting for the
sleep to be over??

I'm using the following 2 lines to abort:
myThread.Abort();
myThread.Join();

Thanks!

Author
31 Jan 2006 6:09 PM
Kevin Spencer
Call the Interrupt method if it is a WaitJoinSleep ThreadState. Then call
Abort.

--
HTH,

Kevin Spencer
Microsoft MVP
..Net Developer
Who is Mighty Abbott?
A twin turret scalawag.

Show quote
"barbutz" <barb***@discussions.microsoft.com> wrote in message
news:095A78D3-2EAD-4526-A485-055E37E61EC8@microsoft.com...
> Hi,
>
> I have a thread which runs in a loop. Between each loop cycle it sleeps
> for
> 10 seconds.
> The problem is that if i'm aborting the thread during its sleep, the
> thread
> will be aborted only after the sleep time is over meaning it can be up to
> 10
> seconds.
>
> Is there a way to force the abortion immediately without waiting for the
> sleep to be over??
>
> I'm using the following 2 lines to abort:
> myThread.Abort();
> myThread.Join();
>
> Thanks!
Author
31 Jan 2006 6:36 PM
barbutz
I'm sorry but that doesn't help. I did this:
if (snmpThread.ThreadState == (System.Threading.ThreadState.WaitSleepJoin |
System.Threading.ThreadState.Background))
   snmpThread.Interrupt();
snmpThread.Abort();
snmpThread.Join();

I checked with the debugger and the Interrupt method is invoked followed by
the Abort method but still the Abort method takes too long untill the sleep
ends.

Any suggestion???

Show quote
"Kevin Spencer" wrote:

> Call the Interrupt method if it is a WaitJoinSleep ThreadState. Then call
> Abort.
>
> --
> HTH,
>
> Kevin Spencer
> Microsoft MVP
> ..Net Developer
> Who is Mighty Abbott?
> A twin turret scalawag.
>
> "barbutz" <barb***@discussions.microsoft.com> wrote in message
> news:095A78D3-2EAD-4526-A485-055E37E61EC8@microsoft.com...
> > Hi,
> >
> > I have a thread which runs in a loop. Between each loop cycle it sleeps
> > for
> > 10 seconds.
> > The problem is that if i'm aborting the thread during its sleep, the
> > thread
> > will be aborted only after the sleep time is over meaning it can be up to
> > 10
> > seconds.
> >
> > Is there a way to force the abortion immediately without waiting for the
> > sleep to be over??
> >
> > I'm using the following 2 lines to abort:
> > myThread.Abort();
> > myThread.Join();
> >
> > Thanks!
>
>
>
Author
31 Jan 2006 6:52 PM
Michael Nemtsev
Hello barbutz,

Thus move logic to separate appDomain and reload whole domain, if nothing
better helps

b> I'm sorry but that doesn't help. I did this:
b> if (snmpThread.ThreadState ==
b> (System.Threading.ThreadState.WaitSleepJoin |
b> System.Threading.ThreadState.Background))
b> snmpThread.Interrupt();
b> snmpThread.Abort();
b> snmpThread.Join();
b>
b> I checked with the debugger and the Interrupt method is invoked
b> followed by the Abort method but still the Abort method takes too
b> long untill the sleep ends.
b>
b> Any suggestion???


---
WBR,
Michael  Nemtsev :: blog: http://spaces.msn.com/members/laflour

"At times one remains faithful to a cause only because its opponents do not
cease to be insipid." (c) Friedrich Nietzsche
Author
31 Jan 2006 7:41 PM
Jon Skeet [C# MVP]
barbutz <barb***@discussions.microsoft.com> wrote:
> I'm sorry but that doesn't help. I did this:
> if (snmpThread.ThreadState == (System.Threading.ThreadState.WaitSleepJoin |
> System.Threading.ThreadState.Background))
>    snmpThread.Interrupt();
> snmpThread.Abort();
> snmpThread.Join();
>
> I checked with the debugger and the Interrupt method is invoked followed by
> the Abort method but still the Abort method takes too long untill the sleep
> ends.
>
> Any suggestion???

Yes - don't call Sleep in the first place, or Abort either.

See http://www.pobox.com/~skeet/csharp/threads/shutdown.shtml

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Author
31 Jan 2006 8:09 PM
barbutz
What do you mean by "don't call sleep or Abort"? My thread work loop is doing
something like that:
while (true)
{
    // DO Something
    Thread.Sleep(10000);
}

I don't want the thread to be over by itself i want to stop it immediately
and not to raise some "stop" flag and wait for it to stop gracefully (as in
the article you pointed).
The thing is that the Abort method does not abort the thread while in sleep
state. I tried the Interrupt method as Kevin suggested and according to the
MSDN this method should interrupt a sleep state but it doesn't so i don't
know what else to do.

Show quote
"Jon Skeet [C# MVP]" wrote:

> barbutz <barb***@discussions.microsoft.com> wrote:
> > I'm sorry but that doesn't help. I did this:
> > if (snmpThread.ThreadState == (System.Threading.ThreadState.WaitSleepJoin |
> > System.Threading.ThreadState.Background))
> >    snmpThread.Interrupt();
> > snmpThread.Abort();
> > snmpThread.Join();
> >
> > I checked with the debugger and the Interrupt method is invoked followed by
> > the Abort method but still the Abort method takes too long untill the sleep
> > ends.
> >
> > Any suggestion???
>
> Yes - don't call Sleep in the first place, or Abort either.
>
> See http://www.pobox.com/~skeet/csharp/threads/shutdown.shtml
>
> --
> Jon Skeet - <sk***@pobox.com>
> http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
> If replying to the group, please do not mail me too
>
Author
31 Jan 2006 8:24 PM
Jon Skeet [C# MVP]
barbutz <barb***@discussions.microsoft.com> wrote:
> What do you mean by "don't call sleep or Abort"? My thread work loop is doing
> something like that:
> while (true)
> {
>     // DO Something
>     Thread.Sleep(10000);
> }
>
> I don't want the thread to be over by itself i want to stop it immediately
> and not to raise some "stop" flag and wait for it to stop gracefully (as in
> the article you pointed).

And did you read *why* a graceful stop is preferrable?

> The thing is that the Abort method does not abort the thread while in sleep
> state. I tried the Interrupt method as Kevin suggested and according to the
> MSDN this method should interrupt a sleep state but it doesn't so i don't
> know what else to do.

Well you could try reading the article I linked to before once again...
even if you're still going to call Abort (which I urge you not to do
unless you're taking down the AppDomain) you can still use the rest of
the information in there - use Monitor.Wait or a WaitHandle of some
description instead of Sleep, and then you just need to pulse the
appropriate monitor or set the event after you've called Thread.Abort.

You need to accept, however, that calling Abort is not going to
guarantee an immediate abort. If you're in unmanaged code for whatever
reason, it won't abort the thread until that finishes anyway.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet   Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

AddThis Social Bookmark Button