Program code
Backgroundworker component
Added the backgroundworker component in vs2005, which is very convenient to use in multi-threaded programming. However, at the beginning, it took a lot of detours because it was not clear about its usage mechanism, now I will share with you my experience in using it.
This attribute, method, and event are mainly used in the backgroundworker class:
Important attributes:
1. cancellationpending gets a value indicating whether the application has requested to cancel background operations. By judging the cancellationpending attribute in the dowork event, you can determine whether the background operation needs to be canceled (that is, the end thread );
2. isbusy gets a value indicating whether the backgroundworker is running an asynchronous operation. The isbusy attribute is used in the program to determine whether background operations are in use;
3. workerreportsprogress gets or sets a value that indicates whether the backgroundworker can report progress updates.
4. workersuppscanscancellation gets or sets a value that indicates whether backgroundworker supports asynchronous cancellation. Set workersuppscanscancellation to true so that the program can call the cancelasync method to submit a request to terminate the suspended background operation;
Important methods:
1. cancelasync requests to cancel pending background operations
2. runworkerasync starts to perform background operations.
3. reportprogress raises the progresschanged event.
Important events:
1. occurs when dowork calls runworkerasync.
2. progresschanged when reportprogress is called
3. runworkercompleted occurs when the background operation is completed, canceled, or exception is thrown.
There are also three important parameters: runworkercompletedeventargs, doworkeventargs, and progresschangedeventargs.
The Calling mechanism and sequence of attributes, methods, and events of backgroundworker:
It can be seen that three important parameter transfer processes occurred throughout the life cycle:
Parameter Pass 1: This Parameter Pass is to pass the object in runworkerasync (object) to the doworkeventargs of the dowork event. argument, because only one parameter can be passed here, in actual application, encapsulate a class, and pass the entire instantiated class as the object of runworkerasync to doworkeventargs. argument;
Parameter transfer 2: This is to pass the program running progress to the progresschanged event. In actual use, it is often used to update the progress bar or log information for methods and events;
Parameter transfer 3: Before the dowork event ends, the result data generated by the background thread is assigned to doworkeventargs. result, while calling the runworkercompleted event runworkercompletedeventargs. Result attribute in the runworkercompleted event to obtain the result generated by the background thread.
In addition, we can see that the dowork event is run in the background thread, so the user interface content cannot be operated in this event. If you need to update the user interface, you can use the progresschanged event and runworkcompleted event.
Winform often encounters some time-consuming operation interfaces, such as counting the number of folders or files in a disk partition. If the partition is large or there are too many files, if the processing is not good, it may cause a "false" situation or an exception that "Inter-thread operation is invalid". To solve this problem, you can use a delegate to handle it. the backgroundworker class can also be used in net2.0.
The backgroundworker class is a newly added class in. NET 2.0. This class can be used when long operations are required without long waits.
Make sure that no user interface objects are operated in the dowork event handler. You should use the progresschanged and runworkercompleted events to communicate with the user interface.
Using system;
Using system. Collections. Generic;
Using system. componentmodel;
Using system. Data;
Using system. drawing;
Using system. text;
Using system. Windows. forms;
/*
* Author: Zhou Gong
* The backgroundworker class is a newly added class in. NET 2.0. This class can be used when long operations are required without long waits.
* Make sure that no user interface objects are operated in the dowork event handler. You should use the progresschanged and runworkercompleted events to communicate with the user interface.
* It has several attributes:
* Cancellationpending -- indicates whether the application has requested to cancel background operations.
* Isbusy -- indicates whether the backgroundworker is running an asynchronous operation.
* Workerreportsprogress -- this value indicates whether backgroundworker can report progress updates.
* Workersuppscanscancellation -- this value indicates whether backgroundworker supports asynchronous cancellation.
* There are also the following events:
* Dowork -- occurs when runworkerasync is called.
* Progresschanged -- this occurs when reportprogress is called.
* Runworkercompleted: this operation occurs when the background operation is completed, canceled, or an exception is thrown.
*
* There are also the following methods:
* Cancelasync -- request to cancel pending background operations
* Reportprogress -- raises the progresschanged event
* Runworkerasync -- start to perform background operations
*
**/
Namespace backgroundworkerdemo
{
Public partial class mainform: Form
{
Private backgroundworker worker = new backgroundworker ();
Public mainform ()
{
Initializecomponent ();
Worker. workerreportsprogress = true;
Worker. workersuppscanscancellation = true;
// Place for formal operations
Worker. dowork + = new doworkeventhandler (dowork );
// What to do when the task is called, such as prompts
Worker. progresschanged + = new progresschangedeventhandler (progesschanged );
// Report the progress when the task is in progress
Worker. runworkercompleted + = new runworkercompletedeventhandler (completework );
}
// Runworkerasync call occurs
Public void dowork (Object sender, doworkeventargs E)
{
E. Result = computefibonacci (worker, e); // when computefibonacci (worker, e) returns, the asynchronous process ends.
}
// When reportprogress is called
Public void progesschanged (Object sender, progresschangedeventargs E)
{
This. progressbar1.value = E. progresspercentage;
}
// Occurs when the background operation is completed, canceled, or exception is thrown.
Public void completework (Object sender, runworkercompletedeventargs E)
{
MessageBox. Show ("finished! ");
}
Private int computefibonacci (Object sender, doworkeventargs E)
{
For (INT I = 0; I <1000; I ++)
{
If (worker. cancellationpending)
{
E. Cancel = true;
}
Else
{
Worker. reportprogress (I );
}
System. Threading. thread. Sleep (10 );
}
Return-1;
}
Private void btnstart_click (Object sender, eventargs E)
{
Worker. runworkerasync ();
Btnstart. Enabled = false;
Btnpause. Enabled = true;
}
Private void btnpause_click (Object sender, eventargs E)
{
Btnpause. Enabled = false;
Btnstart. Enabled = true;
Worker. cancelasync ();
}
}
}
Http://www.qimao.cn/article.asp? Id = 168