Aborting a thread while on sleep

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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!
 
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.
 
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???
 
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 Nietzsch
 
barbutz said:
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
 
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.
 
barbutz said:
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.
 
Back
Top