C # Async Async and await

Source: Internet
Author: User

1. Page Header <%@ pages language= "C #" autoeventwireup= "true" codefile= "Default.aspx.cs" inherits= "_default" async= " True " %>

2.

Protected Async void Button1_Click (object sender, EventArgs e)
{
TextBox1.Text = new Class1 (). Start ();

int contentlength = await accessthewebasync (); this will block the subsequent execution.
TextBox1.Text + = "1245";
TextBox1.Text + = ContentLength;
}

Async Task<int> accessthewebasync ()// return value type task<t> void Task here
{
HttpClient client = new HttpClient ();
Task<string> getstringtask = client. Getstringasync ("http://msdn.microsoft.com");

Doindependentwork ();

String urlcontent = await getstringtask;

return urlcontent.length;

}

void Doindependentwork ()
{
TextBox1.Text + = "working ...";
}

The second way:

public string Start ()
{
Diaplay (); //This will not prevent thread execution
return result. ToString () + "12454";
}

Public async void Diaplay ()
{
Double result = await getvalueasync (124.12, 1.122);

}

task<double> Getvalueasync (double num1, double num2)
{
Return Task.run (() =
{
for (int i = 0; i < 10000; i++)
{
NUM1 = num1/num2;
}
return NUM1;
});
}

C # Async Async and await

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.