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. PHP判断当前访问的是 微信、iphone、android 浏览器

    <?phpvar ua = navigator.userAgent.toLowerCase(); if (ua.match(/MicroMessenger/i)=="micromess ...

  2. Oracle资源

    ORACLE 10g下载地址 oracle 下载还需要用户名我自己注册了个方便大家使用下载 user:1603869780@qq.compass:qwe123QWE现在直接点击不能下载了 要经过ora ...

  3. loopback 05

    数据并发处理 数据库事务 事务隔离 ACID性质 原子性(Atomicity): 要么全部被执行,要么都不执行; 一致性(Consistency): 满足完整性约束; 隔离性(Isolation): ...

  4. 学习使用MAC

    Mac:指苹果笔记本.台式机.分为:MacBook Air.MacBook Pro.iMac.Mac mini和Mac Pro.有时也指苹果电脑操作系统,分:雪豹.山狮.巨浪和优山美地. iOS:指苹 ...

  5. 一个java集合使用bug

    在使用java集合的时候有的时候集合是来自于一些第三方工具提供的从字符串或json 转出集合的对象有时是抽象类,这时的对象部分功能是未实现的,在使用相应操作的时侯 会引发bug. Exception  ...

  6. localhost和127.0.0.1 的区别

  7. win dos命令行设置ip和dns

    首先以管理员身份运行cmd,保证之后在黑框中输入的命令都有管理员权限 C:\Windows\system32>netsh netsh>int netsh interface>ip n ...

  8. DOM基础3

    隔行变色 <!DOCTYPE html> <html> <head lang="en"> <meta charset="UTF- ...

  9. 关于fragment保存变量的问题

    之前遇到一个问题:某个fragment在打开改变状态好后,然后关闭,要求是再次打开时该状态依然保留 这时候求度娘.自己解决问题后,现在整理过程如下: 1.新定义Bundle saveState=new ...

  10. [转]error while loading shared libraries 错误解决办法总结

    http://blog.csdn.net/wallwind/article/details/7580659 错误信息: error while loading shared libraries: li ...