0

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?

void commThread::ThreadEntryPoint1()
{
     while(1)
     {
                //do work
	Sleep(15000);  //15 second sleep
     }
}

and in my mainform destructor which controls the thread:

~Form1()
		{
			if (components)
			{
				delete components;
			}

			if(Thread1->IsAlive)
			{
                                                                //Thread1->Interrupt();
				Thread1->Abort();
			}
		}

Thanks.

3
Contributors
2
Replies
3
Views
9 Years
Discussion Span
Last Post by Tilir
0

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...

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.