Friday, October 29, 2010

Asynchronous Programming in C#

Making Asynchronous Programming Easy

Asynchronous Programming in C#

[csharp]public async void AsyncIntroParallel()
{
Task<string> page1 = new WebClient().DownloadStringTaskAsync(new Uri("http://www.weather.gov"));
Task<string> page2 = new WebClient().DownloadStringTaskAsync(new Uri("http://www.weather.gov/climate/"));
Task<string> page3 = new WebClient().DownloadStringTaskAsync(new Uri("http://www.weather.gov/rss/"));

WriteLinePageTitle(await page1);
WriteLinePageTitle(await page2);
WriteLinePageTitle(await page3);
}[/csharp]

Async CTP has been released. Note: This is not automatically Multi-Threading! From the CTP:

Merely sticking the "Async" modifier will not make a method run on the
background thread. That is correct and by design. If you want to run code on a
background thread, use TaskEx.Run(). Please read the overview for an explanation
of asynchrony.

No comments: