This function is supposed to do an animation asynchronously (without needing to call Dispatcher.Invoke() on everything), then exits the function when the animation is complete.
public static async Task TextSizeAnimationAsync(TextBlock txt, int durationInMilliseconds, double fromSize, double toSize, EventHandler onComplete) { Duration duration = GetDuration(durationInMilliseconds); DoubleAnimation sizeAnimation = new DoubleAnimation(); sizeAnimation.From = fromSize; sizeAnimation.To = toSize; sizeAnimation.Duration = duration; AutoResetEvent signal = new AutoResetEvent(false); sizeAnimation.Completed += (s, e) => { if (onComplete != null) onComplete.Invoke(s, e); signal.Set(); }; txt.BeginAnimation(TextBlock.FontSizeProperty, sizeAnimation); await Task.Yield(); signal.WaitOne(); }
I call this function and expect it to return to the calling thread at
await Task.Yield();
However in debugging, the debugger simply steps to
signal.WaitOne();
...and the UI thread is blocked. Why isn't the UI thread freed? Is it even possible to use ResetEvents in async methods??