The background work thread when the elegant processing window is closed
When the window initializes a background work thread (BackgroundWorker), it is essential to cancel it correctly when the window is closed. Otherwise, it may cause abnormal or deadly.
Problem description
A window instantiated a backgroundWorker and updated its text box on the main thread. When the closing event of the window is triggered, the BackgroundWorker will be simply canceled and the ObjectDisposexception is abnormal on the infer () call. Instead, waiting for BackgroundWorker to complete in the closing incident will cause dead locks because the Invoke () operation is blocked.
Solution
In order to solve this problem, if BackgroundWorker is still running, the FormClosing event must be canceled. This can be implemented through the following steps:
Treat the onformclosing event in the window.Check whether the BackgroundWorker is busy, if so, cancel it.
Through this method, the window can be turned off without cause abnormal or dead locks, so as to ensure the elegance of the backgroundWorker.
The above is the detailed content of How to Gracefully Shut Down a BackgroundWorker When a Form Closes?. For more information, please follow other related articles on the PHP Chinese website!