Nipun
Nipun

Reputation: 4319

update the UI from a separate thread in wp8 app

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...

  1. Created a separate thread but this thread cannot update the UI as it cannot access the controls

  2. 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

Answers (2)

Stephen Cleary
Stephen Cleary

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

Kevin Gosse
Kevin Gosse

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

Related Questions