Asynchronous in Dotnet4.5
Asynchronous in Dotnet4.5
http://msdn.microsoft.com/en-US/vstudio/
async
http://www.microsoft.com/download/en/de
tails.aspx?displaylang=en&id=9983
http://msdn.microsoft.com/en-us/library/system.threading.tasks.task(v=VS.110).aspx
async await
The async modifier indicates that the method, lambda expression, or
anonymous method that it modifies is asynchronous. Such methods
are referred to as async methods.
An async method provides a convenient way to do potentially longrunning work without blocking the caller's thread. The caller of an
async method can resume its work without waiting for the async
method to finish.
The following example shows the structure of an async method. By
convention, async method names end in "Async.
Typically, a method modified by the async keyword contains at least
one await expression or statement. The method runs synchronously
until it reaches the first await expression, at which point it is
suspended until the awaited task is complete. In the meantime, control
is returned to the caller of the method. If the method does not contain
an await expression or statement, then it executes synchronously. A
compiler warning alerts you to any async methods that do not
contain await.
http://msdn.microsoft.com/en-us/library/hh156513(v=VS.110).aspx
httpRM.EnsureSuccessStatusCode();
// Use ReadAsByteArray to access the content of the resource as a byte array.
return httpRM.Content.ReadAsByteArray();
}
Task Parallelism
The Task Parallel Library (TPL), as its name implies, is based on the concept of the
task. The term task parallelism refers to one or more independent tasks running
concurrently. A task represents an asynchronous operation, and in some ways it
resembles the creation of a new thread or ThreadPool work item, but at a higher level of
abstraction. Tasks provide two primary benefits:
Tasks and the framework built around them provide a rich set of APIs that support
waiting, cancellation, continuations, robust exception handling, detailed status, custom scheduling,
and more.
For both of these reasons, in the .NET Framework 4, tasks are the preferred API for
writing multi-threaded, asynchronous, and parallel code.
http://msdn.microsoft.com/en-us/library/dd537609(v=VS.110).aspx
Async Samples
http://www.wischik.com/lu/AsyncSilverlight/
AsyncSamples.html
C# 4.0
client.DownloadStringCompleted += AsyncIntroSerialBefore_DownloadStringCompleted_1;
client.DownloadStringAsync(new Uri("http://www.weather.gov"));
}
void AsyncIntroSerialBefore_DownloadStringCompleted_1(object sender, DownloadStringCompletedEventArgs e)
{
WriteLinePageTitle(e.Result);
var client = new WebClient();
client.DownloadStringCompleted += AsyncIntroSerialBefore_DownloadStringCompleted_2;
client.DownloadStringAsync(new Uri("http://www.weather.gov/climate/"));
}
void AsyncIntroSerialBefore_DownloadStringCompleted_2(object sender, DownloadStringCompletedEventArgs e)
{
WriteLinePageTitle(e.Result);
var client = new WebClient();
client.DownloadStringCompleted += AsyncIntroSerialBefore_DownloadStringCompleted_3;
client.DownloadStringAsync(new Uri("http://www.weather.gov/rss/"));
}
void AsyncIntroSerialBefore_DownloadStringCompleted_3(object sender, DownloadStringCompletedEventArgs e)
{
WriteLinePageTitle(e.Result);
}
Async
public async void AsyncIntroSerial()
{
var client = new WebClient();
WriteLinePageTitle(await client.DownloadStringTaskAsync(new
Uri("http://www.weather.gov")));
WriteLinePageTitle(await client.DownloadStringTaskAsync(new
Uri("http://www.weather.gov/climate/")));
WriteLinePageTitle(await client.DownloadStringTaskAsync(new
Uri("http://www.weather.gov/rss/")));
}
Resources
http://channel9.msdn.com/Events/BUILD/BUILD2011/TOOL-829T