Introduction to BackgroundWorker Controls
You can use the BackgroundWorker control to perform operations on separate threads in the background, typically for tasks that require background processing, such as database operations, file downloads, and so on. It's easier to use, and if you need less background processing, ① can simply drag a backgroundworker from the toolbox to the main interface in VS to automatically name BackgroundWorker1. ② or Manual declaration Initializes a new instance of the BackgroundWorker class: BackgroundWorker bgw=new BackgroundWorker ();
Owning namespace: System.ComponentModel
1. Common Properties:
①cancellationpending: Gets a value indicating whether the application has requested to cancel the background operation. True if the application has requested to cancel the background operation; otherwise false, the default is False. If Cancellationpending is true, the CancelAsync method has been called on BackgroundWorker.
②isbusy: Gets a value that indicates whether BackgroundWorker is running an asynchronous operation. True if BackgroundWorker is running an asynchronous operation; otherwise false. When RunWorkerAsync is called, BackgroundWorker initiates an asynchronous operation.
③workerreportsprogress: Gets or sets a value that indicates whether BackgroundWorker can report progress updates. True if progress update is supported; otherwise false, the default is False. When this property is true, user code can call the ReportProgress method to raise the ProgressChanged event. You can set it in the BackgroundWorker property, or you can write BGW in your code. Workerreportsprogress = true;
④workersupportscancellation: Gets or sets a value that indicates whether BackgroundWorker supports asynchronous cancellation. True if cancellation is supported, False otherwise, false by default. When this property is true, you can call the CancelAsync method to interrupt the background operation. You can set it in the BackgroundWorker property, or you can write BGW in your code. Workersupportscancellation = true;
2. Common methods:
①cancelasync (): Request to cancel a pending background operation. CancelAsync commits the request to terminate the pending background operation and sets the Cancellationpending property to True.
②dispose (): Releases all resources used by component.
③ondowork (DoWorkEventArgs): Raises the DoWork event.
④onprogresschanged (ProgressChangedEventArgs): Raises the ProgressChanged event.
⑤onrunworkercompleted (Runworkercompletedeventargs): Raises the RunWorkerCompleted event.
⑥reportprogress (Int32), ReportProgress (Int32,object): Raises the ProgressChanged event. e.g:BGW. ReportProgress (percentprogress); The parameter percentprogress represents the percentage of completed background operations, ranging from 0% to 100%.
⑦runworkerasync (), RunWorkerAsync (Object): Begins a background operation. E.g:bgw.runworkerasync (argument); The parameter argument represents the parameters to use for background operations to be performed in the DoWork event handler.
Note that the parameter passed in is at most one, and if you want to pass in more than one parameter, enclose the parameter in a class, pass through an instance of the class, or, if the parameter passed in is the same type, you can write the argument into an array and pass in the array.
3. Events:
①disposed: Occurs when a component is freed by calling the Dispose method.
②dowork: Occurs when RunWorkerAsync is called. The time-consuming operation is called in the handler in this event.
③progresschanged: Occurs when reportprogress is called. The notification that the progress update was received is handled in this event.
④runworkercompleted: Occurs when a background operation is completed, canceled, or an exception is thrown. The notification that is received when the operation completes during this event.
For reference from MSDN, see Msdn:https://msdn.microsoft.com/zh-cn/library/system.componentmodel.backgroundworker (v=vs.110) for more information. aspx
BackgroundWorker control Use (i)