Saturday, August 3, 2013

async

I just finished Chapter 23 of C# 4.0 in a Nutshell which is called "Asynchronous Methods." This stuff is the forerunner of the stuff you can do with async keyword in C# 4.5 which looks like this:

async Task<int> AccessTheWebAsync()
{
   HttpClient client = new HttpClient();
   Task<string> getStringTask = client.GetStringAsync("http://msdn.microsoft.com");
   DoIndependentWork();
   string urlContents = await getStringTask;
   return urlContents.Length;
}

 
 

If this looks simple enough to you, then perhaps you won't be disappointed when I don't show you the 4.0 equivalent. I have only 52 pages left to read in the thousand page reference manual that is C# 4.0 in a Nutshell and I am pretty checked out at this point. The Asynchronous Methods chapter doesn't really help. All of the examples are spaghetti messes. What I take away from the chapter is that I should lean on a tool like NServiceBus before I ever write Asynchronous Methods. The examples also show off scrapping the web as a use case and suggests steering clear of using Asynchronous Methods in file I/O stuff.

 
 

Addendum 3/14/2014: My reference to C# 4.5 should really be a reference to the ASP.NET 4.5 Framework which uses C# 5.0.

No comments:

Post a Comment