In a recent blog post that was since removed, the Visual Basic team let slip an announcement that Visual Basic and C# would be getting a new syntax for asynchronous programming. Built on top of the Task Parallel Library that was introduced in .NET 4, this adds the Async and Await keywords to both languages.
The Async keyword is applied to a function or method. This appears to enable asynchronous behavior in a function. Inside the function, the Await keyword suspends the current method until the following action is completed. The thread itself isn’t suspended; it is released to perform other actions such responding to UI events. Once the asynchronous action is completed, the function continues where it left off.
Here is an example of a typical “search” button for a WPF or WinForms application.
Private Sub SearchButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles SearchButton.Click ProgressBar1.Visibility = Visible SearchButton.IsEnabled = False Dim dt As DataTable = Nothing Dim worker As New BackgroundWorker AddHandler worker.DoWork, Sub() PrepareSearch() worker.ReportProgress(50) dt = SearchDatabase() End Sub AddHandler worker.RunWorkerCompleted, Sub() ResultsGrid.DataContext = dt ProgressBar1.Visibility = Visible SearchButton.IsEnabled = True End Sub AddHandler worker.ProgressChanged, Function(a As Object, b As ProgressChangedEventArgs) _ ProgressBar1.Value = b.ProgressPercentage worker.RunWorkerAsync() End Sub
Based on the blog post, it would look like this in Async VB:
Private Async Sub SearchButton_Click(ByVal sender As Object, ByVal e As RoutedEventArgs) Handles SearchButton.Click ProgressBar1.Visibility = Visible SearchButton.IsEnabled = False Await PrepareSearch() ProgressBar1.Value = 50 ResultsGrid.DataContext = Await SearchDatabaseAsync() ProgressBar1.Visibility = Visible SearchButton.IsEnabled = True End Sub
According to the post, this feature was added to both C# and VB by the same team using the same design, keywords, and unit tests. Thus by conjecture we would expect the C# version to look something like this:
private async void SearchButton_Click(object sender, RoutedEventArgs e) { ProgressBar1.Visibility = Visibility.Visible; SearchButton.IsEnabled = false; await PrepareSearchAsync(); ProgressBar1.Value = 50; ResultsGrid.DataContext = await SearchDatabaseAsync(); ProgressBar1.Visibility = Visibility.Visible; SearchButton.IsEnabled = true; }
In these examples the Async versions of the functions will return a Task object. The code that follows the line with the Await keyword effectively becomes a call back that is executed once the Task object is completed. At this point you may be thinking back to how the CCR library uses C#’s “yield return” to create continuations. This is not accidental, most of the code for “await” has been reused from the code for “yield return”. As a side benefit, the VB team is using this as an excuse to finally complete their iterators feature.
We suspect that we will hear more of the details during the upcoming PDC.