开发者

Notify the UI Thread from Background Thread

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 preferr开发者_JAVA技巧ed 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?


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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜