Reputation: 4319
I am creating a small WP8 app which updates receives data from Windows Azure Mobile Service and show it to user.
Now I have a separate thread whose work is to interact with the azure service get the data and update the UI with the data while the main UI thread is doing other stuff.
I tried the following ways but failed...
Created a separate thread but this thread cannot update the UI as it cannot access the controls
Created a BackgroundWorker
thread, but when I call the Azure Mobile Service await methods the worker thread calls its completed event and then when I call the ProgressChanged
event it fails with exception : operation has already been completed
Can someone please help me?
Upvotes: 2
Views: 1439
Reputation: 456322
Since you are using the async
methods provided by Azure mobile services, you can just use an async
method yourself. No worker thread or background thread is required.
private async Task UpdateUI()
{
var result = await MyAzureMobileServiceCall();
MyUIElement.DataContext = result;
}
Upvotes: 0
Reputation: 39007
You can update the UI from a separate thread by using the BeginInvoke
method of the dispatcher:
Deployment.Current.Dispatcher.BeginInvoke(() =>
{
//Update the UI controls here
});
To be precise, it won't actually update the UI from the separate thread, but it will queue the action so the main thread can execute it when it's available.
Upvotes: 5