✅ Await seemingly ignored inside Dispatcher.BeginInvoke
I'm writing an app in WPF. Currently testing something out, here's the code I'm testing:
What I want to happen is that the Task.Delay inside the BeginInvoke call will ensure that 1 second passes before the code beginning at _nameUpdateTaskRunning = false starts running. However, when I run this code, the focusable/enabled changes never happen, and the visibility changes happen immediately. I've confirmed by breakpoint debugging that the code block inside the if statement happens.
So I'm guessing that either the focusable/enabled changes aren't being applied, or the awaiting of Task.Delay isn't happening properly and the code keeps running and does the next Dispatcher.Invoke immediately. Does anyone know which it is, and what I can do to fix it?
2 Replies
unfortunately this is a bit of a tricky situation
the dispatcher will mark the operation you enqueued as “done” once the delegate you passed to it returns
when you
await
, it returns from the function (that’s the mechanism by which async works)
so, once you hit the first await, the dispatcher will think the operation you queued is done. which, of course, is true for synchronous methods, but it’s not helpful here
i would use a TaskCompletionSource
actually, ignore that, there’s a better way of doing this. how about:
this will await the value returned by the delegate instead of the completion of the dispatcher operation
of course, this means that it’ll be synchronously waiting for until the first await, i’m not sure if this matters to youhmm, it works, and I don't think the synchronous waiting should be too much of a problem
I'll go for that second solution, I think. Thanks!