Skip to main content

Asynchronous Programming in C#

·97 words·1 min
Darren Pruitt
Author
Darren Pruitt

Making Asynchronous Programming Easy

Asynchronous Programming in C#

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);  
}

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.