Two Methods of C # asynchronous programming

Source: Internet
Author: User

1. Traditional begininvoke method.

The begininvoke method is used to start the C # asynchronous call. It returns iasyncresult and can be used to monitor the call progress. The endinvoke method is used to retrieve the C # asynchronous call result.

After begininvoke is called, you can call the endinvoke method at any time. If the C # asynchronous call is not completed, endinvoke will be blocked until the C # asynchronous call is completed.

To sum up its usage, there are five steps:

1. Declaration Commission

2. Create an Asynchronous Method

3. instantiation Commission (associating Commission with method)

4. Call the asynchronous method through the instance begininvoke

5. End the asynchronous call method through the instance endinvoke

Instance:

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. threading. tasks; using system. windows. forms; namespace asyncandawait {public partial class form2: FORM {public form2 () {initializecomponent ();} // 1. declare the delegate Public Delegate void ass (); // 2. create an Asynchronous Method private void event1 () {console. writeline ("event1 start"); system. threading. thread. sleep (1, 4000); console. writeline ("event1 end");} private void button#click (Object sender, eventargs e) {// 3. instance Commission (Association Method) ass myass = new ass (event1); // 4. call the Asynchronous Method iasyncresult IA = myass through the instance begininvoke. begininvoke (null, null); // 5. end the asynchronous call method myass through the instance endinvoke. endinvoke (IA );}}}

Ii. Net 4.5 introduces two new keywords async and await asynchronous call Methods

Async and await keywords are the core of C # asynchronous programming. By using these two keywords, you can use. NET Framework

Or an Asynchronous Method for Windows runtime resources is as easy as creating a synchronous method. By Using async and await

The defined Asynchronous Method is called Asynchronous Method.

The following example shows an Asynchronous Method. Almost everything in the Code should look like you're not familiar

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. threading. tasks; using system. windows. forms; namespace asyncandawait {public partial class form3: FORM {public form3 () {initializecomponent () ;}// declare the Asynchronous Method private async task <int> event1 () {console. writeline ("START"); await task. delay (4000); console. writeline ("end"); return 2;} private async void button#click (Object sender, eventargs e) {// call Asynchronous Method -- Method 1 int back = await event1 (); messageBox. show (back. tostring (); // call the Asynchronous Method -- method 2 task <int> back1 = event1 (); // you can perform operations that do not depend on the return value. int back2 = await back1; MessageBox. show (back2.tostring ());}}}

In the above example, the await task. Delay (100) statement is used in the event1 method, which is added to make asyncwork An Asynchronous Method,

What if the operation you want to perform does not support await modification? In fact, it is very simple to use task. Factory. startnew (). For example:

Using system; using system. collections. generic; using system. componentmodel; using system. data; using system. drawing; using system. LINQ; using system. text; using system. threading. tasks; using system. windows. forms; namespace asyncandawait {public partial class form4: FORM {public form4 () {initializecomponent ();} // declare the Asynchronous Method // if the operation you want to perform does not support await modification, it is actually very simple to use the task. factory. startnew (). For example, private async task <int> event1 () {int input = 4000; console. writeline ("event1 start"); await task. factory. startnew (func <object, int>) event2, input); console. writeline ("event1 end"); return 2;} // synchronous method, does not support await modification. To asynchronously call an input parameter, it must be of the object type, private int event2 (Object input) {int val = (INT) input; system. threading. thread. sleep (VAL); return 2;} private async void button#click (Object sender, eventargs E) {// call the Asynchronous Method -- Method 1 // int back = await event1 (); // MessageBox. show (back. tostring (); // call the Asynchronous Method -- method 2 task <int> back1 = event1 (); // you can perform operations that do not depend on the return value. int back2 = await back1; MessageBox. show (back2.tostring ());}}}

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.