Asynchronous Data Calls in Windows Store Apps
Below is the way that I’ve written my (MVVM, C#, XAML) Windows store app to make asynchronous data calls. This is necessary so that the UI doesn’t lock up while some network action or request is being performed.
Make a function that gets your data synchronously. This makes it simpler to write, read, and test.
Then, in the function where I want to make the call, I create a task to run the function asynchronously, and set up a Completed handler. Use ThreadPool.RunAsync
to run it asynchronously. I have a function called RunOnComplete(Action)
that helps me invoke any actions on the UI thread when the task is complete. This helps when I'm updating some data that's going show on the UI.
I mention the reference to DispatchHelper
in this post.