With .NET 4.5 we got async and I’ve listened to some shows on async. Now I wanted to try it out to see what it meant. Played around a little with it and set up a small test project.

What I understand is that asynchronous is not about parallel background threads but about calls being made but not getting a response in necessarily the same order.

I put together the following code after checking out a couple of blog posts, one from Scott Hanselman and one from Asp.net.

What I’m trying to see is when a call gets made and when a response is handled.

Stopwatch timer;

protected void Page_Load( object sender, EventArgs e)
{
	timer = Stopwatch.StartNew();
	LogBox.Text = "Start async calls " + timer.ElapsedTicks + Environment.NewLine;
	RegisterAsyncTask( new PageAsyncTask(LoadSomeData));
	LogBox.Text += "End async calls " + timer.ElapsedTicks + Environment.NewLine;

}

public async Task LoadSomeData()
{
	HttpClient client = new HttpClient ();
   
	LogBox.Text += "Making Sample 1 call " + timer.ElapsedTicks + Environment.NewLine;
	var sample1 = client.GetStringAsync("http://localhost:51510/testpage.aspx" );
	LogBox.Text += "Sample 1 call has been made " + timer.ElapsedTicks + Environment.NewLine;
	LogBox.Text += "Making Sample 2 call " + timer.ElapsedTicks + Environment.NewLine;
	var sample2 = client.GetStringAsync("http://localhost:51510/testpage.aspx" );
	LogBox.Text += "Sample 2 call has been made " + timer.ElapsedTicks + Environment.NewLine;
	LogBox.Text += "Making Sample 3 call " + timer.ElapsedTicks + Environment.NewLine;
	var sample3 = client.GetStringAsync("http://localhost:51510/testpage.aspx" );
	LogBox.Text += "Sample 3 call has been made " + timer.ElapsedTicks + Environment.NewLine;

	LogBox.Text += "Sample 1 await " + timer.ElapsedTicks + Environment.NewLine;
	var sample1output = await sample1;
	LogBox.Text += "Sample 2 await " + timer.ElapsedTicks + Environment.NewLine;
	var sample2output = await sample2;
	LogBox.Text += "Sample 3 await " + timer.ElapsedTicks + Environment.NewLine;
	var sample3output = await sample3;
	LogBox.Text += "await passed " + timer.ElapsedTicks + Environment.NewLine;

	foreach ( var sample in sample1output)
	{
		Async1.Text += sample + ". ";
	}
	foreach ( var sample in sample2output)
	{
		Async2.Text += sample + ". ";
	}
	foreach ( var sample in sample3output)
	{
		Async3.Text += sample + ". ";
	}
}

The output I got was this:

Start async calls 1
End async calls 16
Making Sample 1 call 18381
Sample 1 call has been made 26325
Making Sample 2 call 26332
Sample 2 call has been made 26381
Making Sample 3 call 26385
Sample 3 call has been made 26424
Sample 1 await 26428
Sample 2 await 8869474
Sample 3 await 8869491
await passed 8869497

What I can see from this is that the async call in Page_Load is done and Page_Load finishes but then the asynchronous calls in my load function is returning and finishing up after awhile.

Very simple and not very scientific test but I think I’m starting to get a hang of it. Need to do some more testing.

Advertisements