Cook Computing

Anders Hejlsberg Session on C#/VB Future Directions

September 18, 2011 Written by Charles Cook

I'm working my way through some of the more interesting Build conference sessions and this morning I watched the Future directions for C# and Visual Basic talk by Anders Hejlsberg. He's a very good presenter and the demos all worked unlike those in some of the other sessions I've watched. The section on the Roslyn "compiler as a service" project had some very cool demos involving smart refactoring, writing C# code via an interactive window, complete with intellisense, and pasting VB code into a C# file and seeing the code automatically transformed to C#. Well worth watching as a glimpse of the possibilities that will be opened up for IDE tools in the future.

The section on asynchronous programming was a recap of previous talks over the last year but it inspired me to write my first Metro app to investigate how the new async support in C# 5 will make it easier to call multiple asynchronous calls. The scenario I came across in a recent Silverlight project was having to display a busy indicator (now provided by the Windows Runtime ProgressRing class) while asynchronous calls are being made and always ensure the busy indicator was switched off when the calls finished, regardless of whether one of them failed. The pattern used to implement this resulted in code with something like the following (simplified) structure:

private void Button_Click(object sender, RoutedEventArgs e)
{
    BusyIndicator.IsActive = true;
    try
    {
        RemoteCall1((result1) =>
        {
            try
            {
                // ... process result of first call
                RemoteCall2((result2) =>
                {
                    try
                    {
                        // ... process result of second call
                    }
                    catch (Exception ex)
                    {
                        StatusText.Text = ex.Message;
                    }
                    finally
                    {
                        BusyIndicator.IsActive = false;
                    }
                });
            }
            catch (Exception)
            {
                StatusText.Text = ex.Message;
                BusyIndicator.IsActive = false;
            }
        });
    }
    catch (Exception)
    {
        StatusText.Text = ex.Message;
        BusyIndicator.IsActive = false;
    }
}

The code is ugly and error-prone, for example a bug which occurred more than once was omitting to switch off the busy indicator in the event of an exception being thrown. The async support in C# transforms this into something much simpler (assuming C#5-style async versions of the remote calls are available):

async void Button_Click(object sender, RoutedEventArgs e)
{
      try
      {
            BusyIndicator.IsActive = true;
            var result1 = await RemoteCall1Async();
            // ... process result of first call
            var result2 = await RemoteCall2Async();
            // ... process result of second call
            StatusText.Text = "completed successfully";
      }
      catch (Exception ex)
      {
            StatusText.Text = ex.Message;
      }
      finally
      {
            this.BusyIndicator.IsActive = false;
      }
}