Await

One of the wonderful things about the new Windows Runtime is that it provides a comprehensive means for accessing the native Windows platform while avoiding the very real usability issues most of us found when using the traditional Platform Invoke. Thankfully the .NET framework can share the Windows Runtime features exactly, and it makes accessing devices like cameras, storage, NFC, or simply getting access to the internet, consistent and easy. One of my favorite keywords by far is await here is an example:

private async Task GetPageBody()
{
	// From System.Net.Http namespace.
	HttpClient client = new HttpClient();
	Task<string> getBodyTask = client.GetStringAsync("http://www.PoppaString.com");
	string body = await getBodyTask;
}

The above method is an example of an async method and this example contains a single await expression (could contain more). You apply the await operator to an operand in an asynchronous method or lambda expression to suspend execution of the method until the awaited task completes. An await expression never blocks the thread upon which it is executing, it causes the compiler to treat the balance of the asynchronous method as a continuation of the awaited task. Control returns to the caller of the asynchronous method, but when the task completes it invokes the continuation, and executes the balance of the asynchronous method.

Fast and fluid! I have started using this everywhere!



Comment Section



Comments are closed.