Async and populating data for WPF application

async-await, c#, mvvm, mvvm-light, wpf

Solution

You're seeing the classic deadlock situation that I describe on my blog.

To solve it, use `async` all the way, as I describe in my MSDN article on `async` best practices.

This can be difficult in some scenarios. I have a blog post describing a few approaches for `async` constructors. However, since you're talking about a ViewModel and data for the UI, you'll probably find my blog post on `async` properties more helpful - in particular, the section on data binding.

Problem

I have a WPF application that initializes the state of the UI via methods in the constructor. However, it's never returning from the `Wait();` in the constructor. Here's what I am currently doing via a fairly-contrived sample: ``` public class SomeViewModel { public ICommand AddDataCommand { get { return RelayCommand(AddDataExecute); } } public ObservableCollection<int> UIData { /* Property with INotifyPropertyChanged */} public SomeViewModel() { //Load synch. here LoadData().Wait(); } public async Task LoadData() { UIData = await Task.Run(() => SomeService.SelectAllUIData()); } public async void AddDataExecute() { //Add new data item to database on separate thread to keep UI responsive await Task.Run(() => SomeService.AddNewData(0)); //Reload data from database and update UI, has to happen after AddNewData completes await LoadData(); } } ``` I believe it's hanging because I am never actually returning a `Task`. However, I don't know of a different way to assign UIData asynchronously that works both in the constructor and the Commands that call it.

Original source

Related problems