(Post) asynchronous call in C #

Source: Internet
Author: User

Asynchronous call, which can be used in C:

Public class testclass
{
// Declare a proxy
Public Delegate void test ();

// Send email information
Public postemail ()
{
//...
}

// Run the test class
Public testrun ()
{
// Create a proxy instance
Test test = new test (postemail );

// Enable asynchronous call to send emails
Test. begininvoke (null, null );

//...
}
}

In this way, the current program process will not be blocked when the postemail function is called. This will undoubtedly speed up the execution process of the program, instead of waiting until the full execution of postemail ends, the testrun function will be executed downward. Asynchronous calling can change the program from sequential execution to concurrent execution. But this also brings about some problems. If there are many links between the program segments of asynchronous calls and the main program segments, the correctness of the program cannot be guaranteed. For example:

Public class testclass
{
// Declare a proxy
Public Delegate void test (ref string emailbody );

// Send the email message and obtain the email content
Public postemail (ref string emailbody)
{
//...
}

// Run the test class
Public testrun ()
{
String emailbody = "";

// Create a proxy instance
Test test = new test (postemail );

// Enable asynchronous call to send emails
Test. begininvoke (ref emailbody, null, null );

// Print the email content
Console. Write (emailbody );

//...
}
}

Here"Print Email contentConsole. Write (emailbody );"The value is uncertain.Because, test. begininvoke (ref emailbody, null, null); called at an uncertain moment ......
We can still use Asynchronous methods for function calls with low Program Logic Relationships.

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.