开发者

Notify the UI Thread from Background Thread

开发者 https://www.devze.com 2023-03-21 04:04 出处:网络
I am trying to download some pages in the background, whose contents will be inserted into a database.

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

精彩评论

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