New Asynchronous Features Enhance .NET Framework 4.5
Developers who have had the opportunity to try out Visual Studio 11 or the Visual Studio Async CTP may be familiar with the new keywords Await and Async in Visual Basic and C#. Recently, Microsoft detailed some of the new changes in .NET Framework 4.5 that will further extend asynchronous programming capabilities.
Writing asynchronous code has become significantly easier in .NET 4.5, and will no longer require very complex methods or callbacks. The structure of new async tasks will look quite familiar to developers; here’s an example of a synchronous method declaration and its asynchronous counterpart:
'synchronous method Private Function GetInfo(url As String) As Byte() 'asynchronous method Private Async Function GetInfo(url As String) As Task(Of Byte())
Asynchronous methods return a task; when Await is called against that task, the method is immediately suspended, and resumes once the task is complete. The structure is simple, but implementing asynchronous code requires a great deal more consideration to avoid decreasing performance.
In addition to the C# and Visual Basic language changes, .NET Framework 4.5 Developer Preview adds a collection of asynchronous methods for common operations. System.IO now includes methods such as ReadAsync, WriteAsync, CopyToAsync, and a few others. Many of the methods in System.Data.Common and System.Data.SqlClient have accompanying asynchronous versions, such as ExecuteQueryAsync, ExecuteScalarAsync, and ReadAsync.
ASP.NET 4.5 allows developers to use HttpRequest.GetBufferlessInputStream asynchronously, and introduces the HttpTaskAsyncHandler to make it easier to write asynchronous handlers and utilize tasks. Windows Communication Foundation (WCF) and Windows Presentation Foundation (WPF) include new asynchronous features as well.
To get started with the new asynchronous features in .NET Framework 4.5 Beta, visit Visual Studio Asynchronous Programming. The framework is available as part of Visual Studio 11, or via the Async CTP for Visual Studio 2010.
Juergen Hoeller Jul 22, 2014