Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / January 2006

Tip: Looking for answers? Try searching our database.

Aborting a thread while on sleep

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
barbutz - 31 Jan 2006 16:21 GMT
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!
Kevin Spencer - 31 Jan 2006 18:09 GMT
Call the Interrupt method if it is a WaitJoinSleep ThreadState. Then call
Abort.

Signature

HTH,

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

> Hi,
>
[quoted text clipped - 15 lines]
>
> Thanks!
barbutz - 31 Jan 2006 18:36 GMT
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???

> Call the Interrupt method if it is a WaitJoinSleep ThreadState. Then call
> Abort.
[quoted text clipped - 18 lines]
> >
> > Thanks!
Michael Nemtsev - 31 Jan 2006 18:52 GMT
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
Jon Skeet [C# MVP] - 31 Jan 2006 19:41 GMT
> I'm sorry but that doesn't help. I did this:
> if (snmpThread.ThreadState == (System.Threading.ThreadState.WaitSleepJoin |
[quoted text clipped - 8 lines]
>
> Any suggestion???

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

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

Signature

Jon Skeet - <skeet@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

barbutz - 31 Jan 2006 20:09 GMT
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.

> > I'm sorry but that doesn't help. I did this:
> > if (snmpThread.ThreadState == (System.Threading.ThreadState.WaitSleepJoin |
[quoted text clipped - 12 lines]
>
> See http://www.pobox.com/~skeet/csharp/threads/shutdown.shtml
Jon Skeet [C# MVP] - 31 Jan 2006 20:24 GMT
> What do you mean by "don't call sleep or Abort"? My thread work loop is doing
> something like that:
[quoted text clipped - 7 lines]
> 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.

Signature

Jon Skeet - <skeet@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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.