What is the use of Deployment.Current.Dispatcher.BeginInvoke( ()=> {…} )?

吃可爱长大的小学妹 提交于 2019-11-27 15:47:09

问题


I have seen this Deployment.Current.Dispatcher.BeginInvoke( ()=> {...} ) format in some code .Is it used to do some work in Background?What are the general uses of it?


回答1:


No, it's not to do work in a background thread - it's to do work on the UI thread. So it's normally called from a background thread, in order to manipulate the UI, which can only be done on the UI thread.

The body of the lambda expression is the code which you want to execute in the UI thread.




回答2:


When code that updates the UI executes from a thread other than the UI thread, an invalid cross-thread access exception occurs.

The dispatcher allows you to pass some code over to the UI thread from another thread.

The project I put in this post demonstrates this, among other concepts.

WebClient, HttpWebRequest and the UI Thread on Windows Phone 7




回答3:


you'll need to use Deployment.Current.Dispatcher in a SilverLight application when you find yourself doing work in a non UI worker thread (within a context that does not inherit from DependencyObject) and you need to update the UI.



来源:https://stackoverflow.com/questions/4679324/what-is-the-use-of-deployment-current-dispatcher-begininvoke

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!