BT
x Your opinion matters! Please fill in the InfoQ Survey about your reading habits!

Sneak Peak: Asynchronous Syntax for Visual Basic and C#

by Jonathan Allen on Oct 28, 2010 |

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.

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Tell us what you think

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Feature Bloat? by John DeHope

My concern is that so much has been added to C# since it's inception that the features are all starting to pile up on themselves. Will we ever get a C# 3000 (as Python did) where they go back to clean things up, breaking backwards compatability?

Re: Feature Bloat? by Stefan Wenig

Or the backwards-incompatible improved java that the Java community is dreaming of?
www.jroller.com/scolebourne/entry/the_next_big_...
Oh, wait, the guy just described c#! Can't be that bad then, can it?

C# has some historic dead weight already, but I find it rather neglectible. It's a modern, interesting language for people who actually care enough to learn a non-trivial language. Hardly a thing in there I'd want to miss.

I wonder how the tradeoff between power and ease of learning is argued for VB though.

C# has officially annouced their version by Jonathan Allen

Re: C# has officially annouced their version by Jonathan Allen

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Email me replies to any of my messages in this thread

4 Discuss

Educational Content

General Feedback
Bugs
Advertising
Editorial
InfoQ.com and all content copyright © 2006-2014 C4Media Inc. InfoQ.com hosted at Contegix, the best ISP we've ever worked with.
Privacy policy
BT