14

I am trying to download some pages in the background, whose contents will be inserted into a database.

I need to do this on a background thread of some kind (either BackgroundWorker or ThreadPool, which is preferred due to the way I can queue things up), but I also need to update the UI when the jobs are done.

How can I notify the UI thread that the jobs are finished on Windows Phone?

I've seen someone use Dispatcher.beginInvoke, but it wasn't clear what he was using (either Worker or Pool)-- is this the correct way of doing this?

ctacke
  • 66,480
  • 18
  • 94
  • 155
Andrew M
  • 4,208
  • 11
  • 42
  • 67

2 Answers2

22
 Deployment.Current.Dispatcher.BeginInvoke(() =>
 {
      // change UI here
 });

Dispatcher allows you to run a piece of code on a thread.

Deployment class provides the application information of a silverlight-based application.

this is the code you need to use, actually this is the way you can run a piece of code on UI thread from another thread (no matter how and where that thread is running).

Mo Valipour
  • 13,286
  • 12
  • 61
  • 87
5

Alternatively, if you're using MVVM, you could update the viewmodel off the UI thread and let the magic of INotifyPropertyChanged handle updating the UI for you.

Matt Lacey
  • 65,560
  • 11
  • 91
  • 143