943,910 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Unsolved
  • Views: 1827
  • C++ RSS
Jul 29th, 2008
0

thread abort

Expand 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?

C++ Syntax (Toggle Plain Text)
  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:

C++ Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 2
Junior Poster in Training
Nemoticchigga is offline Offline
98 posts
since Feb 2008
Jul 29th, 2008
0

Re: thread abort

Instead of Sleep() you can use WaitForSingleObjectEx(), which lets you abort the sleep early.
Sponsor
Team Colleague
Featured Poster
Reputation Points: 5608
Solved Threads: 2282
Retired and Enjoying Life
Ancient Dragon is offline Offline
21,953 posts
since Aug 2005
Jul 29th, 2008
0

Re: thread abort

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...
Reputation Points: 46
Solved Threads: 2
Newbie Poster
Tilir is offline Offline
14 posts
since Jul 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: Problem when using a template classes to store user-defined types
Next Thread in C++ Forum Timeline: Prime Patterns





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC