In our programs, there are often some time-consuming operations. To ensure the user experience, we can make the user interface respond in a timely manner. We generally use multi-threaded operations to make time-consuming operations
The background is complete. For example, we need to display progress bars on the interface when uploading files or other requirements. In. NET2.0, A BackGroundWorker class is provided for us to meet similar requirements.
For usage, refer to MSDN. This article describes how to implement such a function and encapsulate it in a common base class.
1. The encapsulated base classes are as follows:
Public abstract class MyAsyncBaseClass {// encapsulate the private class MyMethodParameters {public string StringParameter {get; set;} public int IntParameter {get; set;} public object CallerStateObject {get; set;} public System. windows. threading. dispatcher CallingDispatcher {get; set ;}// public event EventHandler <MyAsyncEventArgs> MyMethodComplete; protected void FireMyMethodCom PleteEvent (int result, object state) {if (MyMethodComplete! = Null) {MyMethodComplete (this, new MyAsyncEventArgs (result, state) ;}// business logic program public abstract int MyMethod (string stringParameter, int intParameter ); // asynchronous call method public void MyMethodAsync (string stringParameer, int intParameter, object state) {// Instantiate the parameter wrapper. myMethodParameters parameters = new MyMethodParameters () {StringParameter = stringParameer, IntParameter = intParameter, CallerStateObject = state, CallingDispatcher = System. windows. threading. dispatcher. currentDispatcher}; // create the thread System. threading. threadPool. queueUserWorkItem (MyMethodThreaded, parameters);} // The thread entry point private void MyMethodThreaded (object p) {MyMethodParameters parameters = (MyMethodParameters) p; // call the actual MyMethod method int result = MyMethod (parameters. stringParameter, parameters. intParameter); // Incorect way to raise event // This wowould cause a "Cross-thread operation not valid Exception" // FireMyMethodCompleteEvent (result, parameters. callerStateObject); // Correct way to raise event // Construct a new delegate with the signature <int, object> and register // it on the callers event queue. parameters. callingDispatcher. beginInvoke (new Action <int, object> (FireMyMethodCompleteEvent), result, parameters. callerStateObject );}}
2. The event parameters are as follows:
Public class MyAsyncEventArgs: EventArgs {
/// User status object public object State {get; protected set ;}/// result of thread operation completion /// </summary> public int IntResult {get; protected set;} public MyAsyncEventArgs (int result, object state) {State = state; IntResult = result ;}}
3. The classes to be implemented are as follows:
Public class MyClass: MyAsyncBaseClass {public override int MyMethod (string stringParameter, int intParameter) {System. threading. thread. sleep (2000); MessageBox. show ("where the business logic program is executed"); return 42 ;}}
4. perform the following tests:
public partial class Form1 : Form { protected MyAsyncBaseClass class1; public Form1() { InitializeComponent(); class1 = new MyClass();