BackgroundWorker can be used to start a background thread.
Main Events and Parameters:
1.DoWork
--the event is triggered when the Backgroundworker.runworkerasync method is executed, and the DoWorkEventArgs parameter is passed;
2.RunWorkerCompleted
--the event is triggered when an asynchronous operation completes or terminates halfway.
If you need to terminate the background operation prematurely, you can call the Backgroundworker.cancelasync method.
Detects if the Backgroundworker.cancellationpending property is true in the function that handles the DoWork event, and if true, indicates that the user has canceled the asynchronous call while setting the Doworkeventargs.cancel property to True (passed to the second parameter of the function that handles the DoWork event) so that when exiting an asynchronous call, you can let the function that handles the RunWorkerCompleted event know whether to exit normally or in the middle.
3.ProgressChanged
--the processing state change obtained in the manipulation process, triggered by the backgroundworker.reportprogress (int) method, and passed ProgressChangedEventArgs, which contains the percentage of processing, This parameter sets the ProgressBar control on the UI interface.
The Main method:
1. Backgroundworker.runworkerasync
--the "Start" method of asynchronous invocation has two overloads of RunWorkerAsync (), RunWorkerAsync (object argument), and the second overload provides a parameter that can be used by an asynchronous invocation. (If you have more than one parameter to pass, use a class to pass them on). Calling the method triggers the DoWork event and passes the Doworkeventarg parameter for the function that handles the DoWork event, which contains the parameters passed by the RunWorkerAsync. Specific complex operations can be done in the corresponding dowork processing functions.
2. Backgroundworker.reportprogress
--the need to continually feed back progress to the user in a lengthy operation, so that the reportprogress (int percent) can be called, triggering the ProgressChanged event when the ReportProgress method is called. Provides an integer between 0 and 100 that represents the percentage of background activity that has completed. You can also provide any object as a second parameter, allowing you to pass state information to the event handler. As the ProgressChangedEventArgs parameter property passed to this procedure, the percentage and your own object (if provided) are passed to the ProgressChanged event handler. These properties are named Progresspercentage and UserState respectively, and your event handlers can use them in any way they want. (Note: The method is only available if the Backgroundworker.workerreportsprogress property is set to true).
3. Backgroundworker.cancelasync
--but this method is called when the asynchronous call needs to be exited. However, this is not enough because it simply sets the Backgroudworker.cancellationpending property to True. You need to constantly check whether backgroudworker.cancellationpending is true if it is true or quit when the specific asynchronous call is processed. (Note: The method is only available if the Backgroundworker.workersupportscancellation property is set to true).
BackgroundWorker Components
Important attributes:
1. Cancellationpending gets a value indicating whether the application has requested to cancel the background operation. By judging the Cancellationpending property in the DoWork event, you can determine whether you need to cancel the background operation (that is, the end thread);
2. IsBusy gets a value indicating whether the BackgroundWorker is running an asynchronous operation. Use the IsBusy property in the program to determine whether the background operation is in use;
3, WorkerreportsprogressGets or sets a value that indicates whether BackgroundWorker can report progress updates
4, Workersupportscancellation Gets or sets a value that indicates whether BackgroundWorker supports asynchronous cancellation. Setting Workersupportscancellation to True allows a program to invoke the CancelAsync method to submit a request to terminate a pending background operation;
Important methods:
1, CancelAsyncrequest to cancel a pending background operation2, RunWorkerAsyncstart a background operation3, ReportProgressRaising the ProgressChanged event
Important Events:
1. Occurs when DoWork calls RunWorkerAsync
2. Occurs when ProgressChanged calls ReportProgress
3. runworkercompleted occurs when a background operation is completed, canceled, or an exception is thrown
Public Partial classMainwindow:window {PrivateBackgroundWorker M_backgroundworker;//Declare background objects PublicMainWindow () {InitializeComponent (); M_backgroundworker=NewBackgroundWorker ();//instantiating a Background objectm_backgroundworker.workerreportsprogress=true;//setting to advertise progressM_backgroundworker.workersupportscancellation =true;//settings can be canceledm_backgroundworker.dowork+=NewDoworkeventhandler (DoWork); M_backgroundworker.progresschanged+=NewProgresschangedeventhandler (updateprogress); M_backgroundworker.runworkercompleted+=NewRunworkercompletedeventhandler (completedwork); M_backgroundworker.runworkerasync ( This); } voidDoWork (Objectsender, DoWorkEventArgs e) {BackgroundWorker BW= Sender asBackgroundWorker; MainWindow win= E.argument asMainWindow; inti =0; while(I <= - ) { if(BW. cancellationpending) {E.cancel=true; Break; } bw. ReportProgress (i++); Thread.Sleep ( +); } } voidUpdateProgress (Objectsender, ProgressChangedEventArgs e) { intProgress =E.progresspercentage; Label1. Content=string. Format ("{0}", progress); } voidCompletedwork (Objectsender, Runworkercompletedeventargs e) { if(E.error! =NULL) {MessageBox.Show ("Error"); } Else if(e.cancelled) {MessageBox.Show ("Canceled"); } Else{MessageBox.Show ("completed"); } } Private voidButton1_Click (Objectsender, RoutedEventArgs e) {M_backgroundworker.cancelasync (); } }
Use of the "C #" "Thread" BackgroundWorker