Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Make sure new threads close when exit program

Status
Not open for further replies.

dseaver

IS-IT--Management
Jul 13, 2006
467
I have a couple background threads that are semi-infinite loops (aka, if the program closes too early, it will sit the infinite loop in the new thread, the gui will be closed but the program wont). How can I make sure that if the user closes the GUI window that it will stop the threads?
 
What you are suggesting does not sound like a good design and may not be possible anyway. I seem to remember that Windows forces all threads to terminate when the main application thread terminates. It will certainly assume your application has hung if you stop processing windows messages.

The best thing to do is to place the worker processes into a Windows service application.
 
You need a flag on each of your threads that indicates that it needs to end. It needs to check this flag as often as possible.

bool endthread = false;

void DoSomething()
{
if (!endthread)
{
//do a small amount of work
}
}

 
Actually Aptitude - you are right in the fact that it is bad design.

I don't agree with things running in Infinite loops because if your form crashes it may not have the chance to set the flags required which would leave your application running forever. (That is, the microshaft forever, which is about 28 hours before you have to restart your computer anyways)


dseaver, see if there is a way to break up your code so that it is not running in an infinite loop. A thread should do one thing, then die. Then a new thread starts.

 
Ok, I fixed my design by using a background worker and a timer. This question no longer applies. Thanks all
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top