thread abort

Reply

Join Date: Feb 2008
Posts: 98
Reputation: Nemoticchigga is an unknown quantity at this point 
Solved Threads: 2
Nemoticchigga Nemoticchigga is offline Offline
Junior Poster in Training

thread abort

 
0
  #1
Jul 29th, 2008
I am running a thread that does something, then sleeps for a while. If the program closes it continues to sleep to completion. How do I get it to abort immediatly, even if in the middle of sleeping?

  1. void commThread::ThreadEntryPoint1()
  2. {
  3. while(1)
  4. {
  5. //do work
  6. Sleep(15000); //15 second sleep
  7. }
  8. }

and in my mainform destructor which controls the thread:

  1. ~Form1()
  2. {
  3. if (components)
  4. {
  5. delete components;
  6. }
  7.  
  8. if(Thread1->IsAlive)
  9. {
  10. //Thread1->Interrupt();
  11. Thread1->Abort();
  12. }
  13. }

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 16,574
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1612
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: thread abort

 
0
  #2
Jul 29th, 2008
Instead of Sleep() you can use WaitForSingleObjectEx(), which lets you abort the sleep early.
The most important thing in the Olympic Games is not to win but to take part, just as the most important thing in life is not the triumph but the struggle. The essential thing is not to have conquered but to have fought well.
-Pierre de Coubertin, The Olympic Creed Inspired by Bishop Ethelbert
Reply With Quote Quick reply to this message  
Join Date: Jul 2008
Posts: 14
Reputation: Tilir is an unknown quantity at this point 
Solved Threads: 2
Tilir's Avatar
Tilir Tilir is offline Offline
Newbie Poster

Re: thread abort

 
0
  #3
Jul 29th, 2008
Originally Posted by Nemoticchigga View Post
I am running a thread that does something, then sleeps for a while. If the program closes it continues to sleep to completion. How do I get it to abort immediatly, even if in the middle of sleeping?
You may use TerminateThread function, it will terminate your thread immediatly, but it is bad practice. Right decision is to follow Ancient Dragon's recomendation and use some of synchronization objects instead -- for example event. Thread may sleep, waiting some event to be signaled, then awake and abort himself with proper resource cleanup, etc...
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 1212 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C++
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2010 DaniWeb® LLC