Category: C#

Asynchronous lambda expressions

Not only methods but also lambda expressions can be async. Among other things this allows event handlers to be anonymous: sumButton.Click += async (sender, e) => { sumButton.IsEnabled = false; await SumPageSizesAsync(GetUrls()); sumButton.IsEnabled = true; }; AddHandler sumButton.Click, Async Function(sender, e) sumButton.IsEnabled = False Await SumPageSizesAsync(GetUrls()) sumButton.IsEnabled = True End Function Another useful application of

Event handlers and void returning async methods

Async methods can be built from other async methods using await. But where does the asynchrony end? What kind of top level synchronous methods call asynchronous ones to start with? In a client application the typical answer is that asynchronous operations are set off by events. The user clicks a button, and an asynchronous activity

Async methods and awaiting in detail

It is important to understand that async methods like SumPageSizesAsync do not run on their own thread. In fact if you write an async method without any await’s in it, it will be completely synchronous: public async Task<int> TenToSevenAsync() { Thread.Sleep(10000); return 7; } Public Async Function TenToSevenAsync() As Task(Of Integer) Thread.Sleep(10000) Return 7 End

Task based Asynchrony in C# 5.0

The Task and Task<TResult> types are already in the .NET Framework 4. A Task represents an ongoing activity, which may be CPU intensive work running on a separate thread, but may also represent an I/O operation, for example an outstanding response for an internet request. Task represents an activity without a result, and Task<TResult>, which

Whats New in C# 5.0 Part-2

With the proposed new features, the asynchronous version of the code will instead look like this: public async Task<int> SumPageSizesAsync(IList<Uri> uris) { int total = 0; foreach (var uri in uris) { statusText.Text = string.Format("Found {0} bytes ...", total); var data = await new WebClient().DownloadDataAsync(uri); total += data.Length; } statusText.Text = string.Format("Found {0} bytes total",
SiteLock