views:

104

answers:

3

I spawn a thread (only one) to do some work and it pretty much takes care of itself not accessing any data outside of the tread except calling callback() to see if the user wants to quit (also sends a status report back to the main thread to display in the GUI).

When the close closes the exe i would like to wake up the thread and have it quit, whats the best way of doing this? The callback already says if the user wants to quit so now the issue is using Thread.Sleep and waking it up prematurely so it can quit instead of having the process live for another few seconds or minutes. This feature would be nice for stop to exit more quickly.

+2  A: 

Use a BackgroundWorker or set your thread's IsBackground property to true, then it won't keep your application open.

I'd recommend the BackgroundWorker, but the latter is a quick fix.

Update

Original poster changed his requirements after posting the question (see comments attached to this question). Update to answer follows:

If you want to stop a background operation without ending the application, please see Background worker's CancelAsync method. Also, don't use Thread.Sleep - use a WaitHandle and call WaitOne on what you need to wait for.

Mark Byers
I'll looking at it, how do i tell it to wake up from its sleep? -edit- i guess i could use it to not prevent termination but i would like to wake it up when the user clicks stop. I guess i wont bother? (Trying to kill the thread seems bad)
acidzombie24
You shouldn't need to do anything to the background thread. Unless there is some specific cleanup your thread needs to before the application quits, you can just close the application and let the thread die. Background threads can't keep the application open.
Mark Byers
I was more concerned about allowing the user to stop it early and start a different command without waiting in my comment
acidzombie24
I don't understand... if you're more concerned about stopping the thread without closing the application, why didn't you put that in your question? If you need to stop your thread at times other than application termination, you need to use a different solution. Perhaps you should update your question to make that clear, because your current question doesn't reflect your requirements.
Mark Byers
+3  A: 

Another approach would be as follows:

Have a ManualResetEvent in your program and call Set when you want the thread to finish up and close down. Instead of calling Thread.Sleep on your work thread, call event.WaitOne and pass in a TimeSpan object. If the event is signalled, your worker thread will wake up before the timeout occurs - WaitOne will return true.

Dave Cluderay
Even with this solution, I'd still recommend using a BackgroundWorker as well.
Mark Byers
+1  A: 

I have to agree with Mark. The only thing clear about your question is that you need to reexamine your threading strategy. You say you have a thread doing work but then you say you want to wake it up?

Is the thread waiting for work? If so, sleep in shorter cycles and check for exit more often or use a reset event. The best way to wake a thread up is to not put it to sleep. Do what you have to do and exit. Always check for interrupt signals, in whatever form you implement them, before starting any long running operations, and again, if you must sleep the thread, do it in short cycles.

Is the thread busy and you want to interrupt it? You may have no choice but to kill it if you cannot instrument it such that it can respond to interrupt signals in a timely fashion.

Sky Sanders