Home >Backend Development >C++ >How to Gracefully Close a Form with an Active BackgroundWorker?
Elegant closure of the window with an event BackgroundWorker
When the window initializes a backgroundworker of the update window UI, closing the window without encountering abnormal or dead locks may become tricky. This problem highlights the challenges faced when trying to stop BackgroundWorker in the Closing incident of the window.
Question:
Using
to cancel the backgroundWorker in HandleClosingEvent may causebgWorker.CancelAsync()
Invoke()
In HandleClosingEvent, waiting for BackgroundWorker to complete, it will cause dead locks, because ObjectDisposedException
will not return. Invoke()
Solution: Avoiding dead locks and abnormal security methods is to cancel the FormClosing event itself. The following steps outline the solution:
Define a logo to track the user's closing request.In the cover, check whether the BackgroundWorker is busy.
closePending
to true, cancel the backgroundWorker, and set the OnFormClosing
In the closePending
e.Cancel = true
After turning off the operation, reset the RunWorkerCompleted
closePending
sample code: closePending
The above is the detailed content of How to Gracefully Close a Form with an Active BackgroundWorker?. For more information, please follow other related articles on the PHP Chinese website!