Home >Backend Development >C++ >How Can I Gracefully Close a Form with a Running BackgroundWorker?
Elegant closing the background working device: Make sure the window is smooth and close
In the field of multi -threaded programming, managing the complexity of managing background operation is very important. A common situation is that the window uses a background workor (BackgroundWorker), which updates its own text box on the main thread. However, suddenly closing the window may cause abnormal or dead locks.
In order to solve this problem, the interaction between the back -end workspiece and the window closure event must be studied. If the background worksplay is still running during the HandleClosingEvent period, it will blindly cancel it (via bgWorker.cancelasync ()) to cause ObjectDisposexception during the subsequent Invoke call. Instead, waiting for the background workor to complete indefinitely in HandleClosingEvent may lead to a deadlock.
Fortunately, a solution without death and abnormal security is waiting for us. This technology involves the cancellation of the FormClosing incident when the background workspiece is in activity. For this:
Define a Boolean logo (for example, Closepending).
The above is the detailed content of How Can I Gracefully Close a Form with a Running BackgroundWorker?. For more information, please follow other related articles on the PHP Chinese website!