How to await asynchronous tasks in the constructor in C#
For original inspiration see this excellent link by Stephen Cleary: http://blog.stephencleary.com/2013/01/async-oop-2-constructors.html Step 1: Create a new WPF application Step 2: Define your example service In this example, a service to count the number of bytes in a web page: MyStaticService.cs [code language="csharp"] using System; using System.Net.Http; using System.Threading.Tasks; namespace AsyncWpf { public static class MyStaticService { public static async Task<int> CountBytesInUrlAsync(string url) { // Artificial delay to show responsiveness. await Task.Delay(TimeSpan.FromSeconds(3)).ConfigureAwait(false); // Download the actual data and count it. using (var client = new HttpClient()) { var data = await client.GetByteArrayAsync(url).ConfigureAwait(false); return data.Length; } } } } [/code] Step 3:...