About Asynchronous Method call

Source: Internet
Author: User

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();    

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.