Quantcast
Channel: Windows Presentation Foundation (WPF) forum
Viewing all articles
Browse latest Browse all 18858

await Task.Yield() not returning to controller, UI thread blocking (WPF)

$
0
0
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??


Viewing all articles
Browse latest Browse all 18858

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>