We were unable to load Disqus. If you are a moderator please see our troubleshooting guide.

Stephen Cleary • 7 years ago

I think the easiest way to do this is to just not use ConfigureAwait(false) in your ViewModels. In my designs, VMs are the programmatic UI, and as such are expected to run on the UI thread.

public async Task RetrieveData()
{
var data = await GetDataFromSomeWebsite();
Result = data;
}

await takes care of resuming on the UI thread for you, in a dispatcher-agnostic way.

Pedro Lamas • 7 years ago

Fully agree with you Stephen, in all fairness, my example is quite flawed though I've intended this article to be just part 1 of 2 (2nd part will be creating a view model base that can be used in multiple windows with different dispatchers)
I normally either use .ConfigureAwait(false) when I have multiple awaits and then dispatch all UI changes in the end, or bundle those in a separate method where everything is .ConfigureAwait(false) and leave the caller as normal thus returning to the dispatcher.

scbritton • 3 years ago

I'm reeeeeaaaalllly late to the party here. I hope you're all still around, and I also hope that this is still relevant after the changes and upgrades to Visual Studio, .NET, and the various other aspects of dev work. I'm still fairly new to this whole thing.

I'm building c# wpf application that needs to to asynchronously run something in a loop, and update the ui after each pass through the loop. It needs to be async because the UI won't otherwise update until the loop has finished, which is completely useless to me.

I found this post and it seemed to be a good fit for what I'm trying to do, except for one problem: visual studio is telling me, "the namespace "Windows.UI.Core" cannot be found...

So now, officially, "I'm confused." What am I missing here?

Daniel • 7 years ago

Hi Pedro,

Codon FX supports UI thread affinity for INPC. You can find more about it over at: https://www.codeproject.com...

In Codon the UI thread is abstracted as an ISynchronizationContext, which can be resolved via its IoC container. This is especially useful for cross-platform apps.

Cheers,
Daniel