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