How to write a custom awaiter

[This post is part of a series How to await a storyboard, and other things]   The normal behavior of the “await” operator on a task is to suspend execution of the method; then, when the task operand has finished, to resume execution on the same SynchronizationContext and with the same ExecutionContext. You can replace the…

2

How to await a command-line process, and capture its output

[This post is part of a series How to await a storyboard, and other things]   I want to invoke an external executable, and await until it’s finished, and get back its output. (In my case, the external executable is called “tidy.exe” – an excellent open-source utility for turning HTML into clean parseable XML.) To cut a…

2

Await HttpClient.GetStringAsync() and cancellation

I’m a big fan of supporting cancellation on async methods. I was recently wondering how to do this for HttpClient.GetStringAsync(). This post has some answers.   Async Function TestAsync(uri As Uri, Optional cancel As CancellationToken = Nothing                         ) As Task(Of String)     Dim client As New HttpClient     Return Await client.GetStringAsync(uri, cancel)     ‘ error: no overload of GetStringAsync takes a cancellation…

5

How to await a button click

[This post is part of a series How to await a storyboard, and other things]   Sometimes we want to await until a button has been clicked. Once place where this is useful is, for instance, when displaying the message “Click button to continue”.   Await button1.WhenClicked()   Why do it this way? Well, if you did it…

1

How to await a drag operation

[This post is part of a series How to await a storyboard, and other things]   In my app, I want to await a drag operation. Here’s how I’d like to await it:    Private Async Sub rectangle1_PointerPressed(sender As Object,                e As PointerRoutedEventArgs) Handles rectangle1.PointerPressed    Await DragAsync(rectangle1, e)End Sub   Here below is my implementation…

0

How to await a MediaElement (PlaySound in Windows 8)

[This post is part of a series How to await a storyboard, and other things]   Let’s look at making MediaElement awaitable. This is the kind of idiom I’d like to use:   Try     Await mediaElement1.OpenAsync(New Uri(“ms-appx:///Assets/boooo.mp3”))     Await mediaElement1.PlayAsync() Catch ex As Exception     … End Try   Q. Why do I want to await it? Why not just…

0

How to await a storyboard, and other things

This post is part of a series about an important new design pattern, awaiting events… I also made a Channel9 video introduction “Async Over Events“. In this blog series: How to await a storyboard, and other things How to await a MediaElement How to await a drag operation How to await a button-click How to await…

3

How to hibernate async methods (how to serialize Task)

[update: there’s now a sequel to this post, here, which improves the technique so it can handle async callstacks rather than just a single async method at a time.]   Sometimes people ask the question “How can I serialize a Task?” If you try, it throws a SerializationException: Dim t = TestAsync() Using stream As New MemoryStream…

10

Why must async methods return Task?

We all know that async methods return Task or Task(Of T):     Async Function GetNameAsync() As Task(Of String)        Await Task.Delay(10)        Return “ernest”    End Function  Sometimes, advanced users ask for the ability to return different types out of an async method. That’s disallowed: it gives the error message “The Async modifier can only be used on…

7

When is a non-breaking language fix, breaking?

In VS2012 we fixed some method-resolution problems from VS2010. These were method-calls that failed to compile in VS2010, but now compile cleanly in VS2012.   VB: Dim numbers As IEnumerable(Of Integer) = {1, 2, 3}Sub f(Of T)(x As T)Sub f(Of T)(x As IEnumerable(Of T)) f(numbers)  ‘ Should obviously pick the second overload, since it’s more specific…

1