Home >Backend Development >C++ >How to Safely Stop a BackgroundWorker When Closing a Windows Form?
The background work thread when the Windows window is closed elegantly
in the window closing event processing process may cause abnormal or deadlocked locks. bgWorker.CancelAsync()
abnormalities. This is because the window is being released, and the main thread has exited. bgWorker.CancelAsync()
ObjectDisposedException
Question 2: The deadlock when BGWorker is completed
bgWorker
Solution: Coordinate the closure of the window and the background workline to complete
In the window closing the event processing program, if
bgWorker
e.Cancel = true
Set a logo closePending
In the event processing program, check the bgWorker
RunWorkerCompleted
closePending
Close()
This method can prevent abnormalities and dead locks through the completion of the window to close the event and the completion of The above is the detailed content of How to Safely Stop a BackgroundWorker When Closing a Windows Form?. For more information, please follow other related articles on the PHP Chinese website!