Sorry about not being able to give a more precise headline, but here is what I want to do. I have a method foo which will initiate some UI animations. When these animations are finished, I want to signal completion of the method. But instead of signaling completion through an event, I would like to make the method awaitable. How can this be done?
void foo()
{
// start some animations
storyboard.Begin();
storyboard.Completed += (s, e) => { // signal that foo has completed }
}
calling code should be able to write:
await foo();
My actual foo is more complex than illustration above, in that I have a series of animations, which occur one after another, and foo is supposed to complete when all animations are completed.
Generally things that complete based on an event get ‘wrapped’ by using a TaskCompletionSource. Unfortunately, the example on MSDN is (IMHO) overly complicated instead of just being a single instance+event. You’d have something like:
In this scenario, there’s no real need for the TResult, but TaskCompletionSource doesn’t appear to have a non-generic version and I’m not sure what the equivalent pattern would be