One of the most common tasks you need to perform in a Windows Phone application is updating the UI from a separate thread.
 
For example, you may be download some content asynchronously using a WebClient class and when the operation is completed, you want to update the UI with the content that was downloaded. Updating the UI directly from an asynchronous thread is not allowed, as UI controls are not thread-safe.  
 
The easiest way to update the UI from an asynchronous thread is to use the Dispatcher class. To determine if you can update an UI directly, you can use the CheckAccess() method. If this method returns a true, it means you can directly update the UI. Else, you have to use the BeginInvoke() method of the Dispatcher class to update the UI in a thread-safe manner. The following code snippet makes this clear:
 
    if (Dispatcher.CheckAccess() == false)
    {
        //---you have to update the UI through the BeginInvoke() method---
        Dispatcher.BeginInvoke(() =>
            txtStatus.Text = "Something happened..."
        );
    }
    else
    {
        //---you can update the UI directly---
        txtStatus.Text = "Something happened..."
    }
 
If you have more than one statement to perform in the BeginInvoke() method, you can group them into a method and call it like this:
 
        private void UpdateUI(String text)
        {
            txtStatus.Text = text;
            btnLogin.Content = text;
        }
        ...
        ...
 
            Dispatcher.BeginInvoke(() =>
                UpdateUI("Something happened...")
            );

Update UI from an asynchronous thread的更多相关文章

  1. HealthKit开发教程Swift版:起步

    原文:HealthKit Tutorial with Swift: Getting Started 作者:Ernesto García 译者:Mr_cyz ) HealthKit是iOS 8中的新的A ...

  2. Looper分析。ThreadLocal有关

    Class used to run a message loop for a thread. Threads by default do not have a message loop associa ...

  3. How To Use NSOperations and NSOperationQueues

    Update 10/7/14: This tutorial has now been updated for iOS 8 and Swift; check it out! Everyone has h ...

  4. Android Non-UI to UI Thread Communications(Part 1 of 5)

    original:http://www.intertech.com/Blog/android-non-ui-to-ui-thread-communications-part-1-of-5/ ANDRO ...

  5. iOS开发,在main thread以外的thread更新UI

    如果需要在异步任务(Async Task)中更新UI,若直接设置UI,会导致程序崩溃. 例如,在异步block中去更改UI: NSOperationQueue *queue=[[NSOperation ...

  6. Patterns for Asynchronous MVVM Applications: Commands

    Stephen Cleary Download the Code Sample This is the second article in a series on combining async an ...

  7. android 进程/线程管理(三)----Thread,Looper / HandlerThread / IntentService

    Thread,Looper的组合是非常常见的组合方式. Looper可以是和线程绑定的,或者是main looper的一个引用. 下面看看具体app层的使用. 首先定义thread: package ...

  8. Server-Side UI Automation Provider - WinForm Sample

    Server-Side UI Automation Provider - WinForm Sample 2014-09-14 源代码  目录 引用程序集提供程序接口公开服务器端 UI 自动化提供程序从 ...

  9. handler.post 为什么要将thread对象post到handler中执行呢?

    转载网址:http://www.cnblogs.com/crazypebble/archive/2011/03/23/1991829.html在Android中使用Handler和Thread线程执行 ...

随机推荐

  1. SQLite密码添加移除

    背景:电脑清理--个人洁癖 SQLite的最原始的是没有加密的,从而衍生了多种加密算法,但在平常使用中使用System.Data.Sqlite,但其加密后,一般都需要要单独的sqlite管理器--像我 ...

  2. ZLL本地局域网通信过程

    Interface_srpcserver -----以灯的状态操作位例 网关与客户端通过Socket API通信,Socket API在socket_server.c中实现,socket_server ...

  3. 消息队列通信,王明学learn

    消息队列通信 消息队列就是一个消息(一个结构)的链表.而一条消息则可看作一个记录,具有特定的格式.进程可以从中按照一定的规则添加新消息:另一些进程则可以从消息队列中读走消息. 每一个消息都是一个结构体 ...

  4. [荐]jquery插件开发入门

    http://www.cnblogs.com/Wayou/p/jquery_plugin_tutorial.html $.fn.myPlugin = function() { //在这里面,this指 ...

  5. class.forname()用法 转

    主要功能 Class.forName(xxx.xx.xx)返回的是一个类 Class.forName(xxx.xx.xx)的作用是要求JVM查找并加载指定的类, 也就是说JVM会执行该类的静态代码段 ...

  6. express-9 Handlebars模板引擎(2)

    视图和布局 视图通常表现为网站上的各个页面(它也可以表现为页面中AJAX局部加载的内容,或一封电子邮件,或页面上的任何东西).默认情况下,Express会在views子目录中查找视图.布局是一种特殊的 ...

  7. java中new关键字和newInstance()方法有什么区别?

    1.new可以调用带参数的构造函数,newInstance不可以. 2.new 是 java 的关键字,用来构造一个类的实例.而 newInstance 是 Class 的一个方法,不过两个写法的效果 ...

  8. Tomcat 配置 Context

    在 conf/Catalina/localhost/ 下添加 xml配置文件,文件名和站点名一致. 配置文件示例 <?xml version='1.0' encoding='utf-8'?> ...

  9. HDU1853 Cyclic Tour(最小费用最大流)

    题目大概说给一张有向图,每条边都有权值,要选若干条边使其形成若干个环且图上各个点都属于且只属于其中一个环,问选的边的最少权值和是多少. 各点出度=入度=1的图是若干个环,考虑用最小费用最大流: 每个点 ...

  10. android项目 在签名打包遇到的问题

    我在签名打包前,build success  ,可以把程序安装在手机上 然后签名打包的时候,build fail  ,原因采用了release 版本,因此这个时候在gradule build 添加下面 ...