D
David Veeneman
What's the best way to gracefully exit an app while an asynchronous
operation is in progress?
Let's say I have spawned a worker thread using the BackgroundWorker
component. If I exit the application while the worker thread is running, the
app crashes. It doesn't do any good to call CancelAsync() on the worker
thread from a FormClosing event handler--Since the worker thread is running
asynchronously, the main thread will continue the shutdown without waiting
for the cancel to complete.
I could work up some kludge, like cancelling the FormClose, putting a flag
in as a member variable, then reading that in the RunWorkerCompleted()
handler and reinitiating the application shutdown from there. But that seems
awfully clumsy--There must be a more elegant way to pause the application
shutdown until the worker thread ends.
Any ideas? Thanks.
operation is in progress?
Let's say I have spawned a worker thread using the BackgroundWorker
component. If I exit the application while the worker thread is running, the
app crashes. It doesn't do any good to call CancelAsync() on the worker
thread from a FormClosing event handler--Since the worker thread is running
asynchronously, the main thread will continue the shutdown without waiting
for the cancel to complete.
I could work up some kludge, like cancelling the FormClose, putting a flag
in as a member variable, then reading that in the RunWorkerCompleted()
handler and reinitiating the application shutdown from there. But that seems
awfully clumsy--There must be a more elegant way to pause the application
shutdown until the worker thread ends.
Any ideas? Thanks.