C # Use events to Implement Asynchronous calls,

Source: Internet
Author: User

C # Use events to Implement Asynchronous calls,

Question:

The time-consuming operations in the winform program are generally not executed in the UI thread. You need to open another thread. We usually need to display the results on the UI after the time-consuming operation is completed.

The following code calls time-consuming operations in Mainform. cs:

 

1     Job j = new Job();2     j.runJob();


The time-consuming operation is encapsulated in the class Job, and j. runJob () is called to start the time-consuming operation. RunJob encapsulates the code for enabling new threads to execute tasks.

 

After a Job is run, a result is returned and displayed on the UI.

The obvious method is to instantiate a Mainform object in the Job and then directly modify the UI in the Job. This method is highly undesirable and greatly increases the coupling between classes.

You can think of a reasonable method. After the Job class completes a time-consuming task, it sends a "message" to the class that calls it, and the "message" also includes the result that should be displayed. The Job provider should also do this: simply send a "message", regardless of who it is sent. In this way, even if the caller is changed, the code of the Job class does not need to be modified. Such a "message" in C # is an "Event".


Procedure:

1. Declare the two variables in the Job class (the class that needs to call methods in other classes:

 

1 public delegate void FinishHandler (int param); // declare the delegate 2 public event FinishHandler FinishEvent; // declare the event


The delegate variable name can be named at will. The brackets define the type and number of parameters to be passed. An int variable must be passed here.

The event type of the second row must be the same as that of the delegate.

 

2. Call the event in a proper place in the Job class (the class that needs to call methods in other classes:

 

1     FinishEvent(score);


Generally, the operation is time-consuming and the result must be returned.

The parameter format and number in parentheses must be consistent with those defined in step 1.

 

3. register the method to be executed when an event occurs to the event

1     Job j = new Job();    2     j.FinishEvent += updateData;3     j.runJob();


Of course, the parameter form and number of updateData are the same as that of the first step.

 

Analysis summary:

Sort out what happened when running the program.

In actual operation, the first step is to run the three lines of code in step 1.

The second line indicates that the updateData method will be triggered by the FinishEvent event in the j object.


The third line starts to run time-consuming tasks. The Job class comes to the line of code in step 2 when it is executed till the end of the task to prepare and return data, all methods that have registered the event are triggered to start execution.
In this way, from the MainForm perspective, asynchronous calling is implemented, and the results of time-consuming tasks are read through another function.

From the Job class perspective, the method in another class is called.

More applications are found in future programming practices.

 

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.