Demo source code
Background
Some small programs or demos often use Asynchronization and multithreading to execute some time-consuming work and timely feedback the progress. I usually use the BackgroundWorker Control located in the [System. ComponentModel] namespace. Although the properties and events of the control are simple, I will always forget it. Therefore, the purpose of this article is to encapsulate the BackgroundWorker control. I only need to pay attention to what I want to do (DoWork), what I want to do (RunWorkerCompleted), and the progress feedback (ProgressChanged.
Project Structure Overview
The following is the Demo project structure:
Simplest call
The encapsulated BackgroundWorker class provides a default implementation for each event. Therefore, it is especially simple to use in projects in the console test project. See the following code:
BackgroundWorker bw =
Running:
Summary
It's just a small package, and the code is very simple. From then on, our partners can use this package to avoid forgetting it, and use it more concisely!