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