UI线程中使用

public class SmartDispatcher

{

    public static void BeginInvoke(Action action)

    {

        if (Deployment.Current.Dispatcher.CheckAccess() 

            || DesignerProperties.IsInDesignTool)

        {

            action();

        }

        else

        {

            Deployment.Current.Dispatcher.BeginInvoke(action);

        }

    }

}

 
 
using System.ComponentModel;

 

namespace System.Windows.Threading

{

    /// <summary>

    /// A smart dispatcher system for routing actions to the user interface

    /// thread.

    /// </summary>

    public static class SmartDispatcher

    {

        /// <summary>

        /// A single Dispatcher instance to marshall actions to the user

        /// interface thread.

        /// </summary>

        private static Dispatcher _instance;

 

        /// <summary>

        /// Backing field for a value indicating whether this is a design-time

        /// environment.

        /// </summary>

        private static bool? _designer;

         

        /// <summary>

        /// Requires an instance and attempts to find a Dispatcher if one has

        /// not yet been set.

        /// </summary>

        private static void RequireInstance()

        {

            if (_designer == null)

            {

                _designer = DesignerProperties.IsInDesignTool;

            }

 

            // Design-time is more of a no-op, won't be able to resolve the

            // dispatcher if it isn't already set in these situations.

            if (_designer == true)

            {

                return;

            }

 

            // Attempt to use the RootVisual of the plugin to retrieve a

            // dispatcher instance. This call will only succeed if the current

            // thread is the UI thread.

            try

            {

                _instance = Application.Current.RootVisual.Dispatcher;

            }

            catch (Exception e)

            {

                throw new InvalidOperationException("The first time SmartDispatcher is used must be from a user interface thread. Consider having the application call Initialize, with or without an instance.", e);

            }

 

            if (_instance == null)

            {

                throw new InvalidOperationException("Unable to find a suitable Dispatcher instance.");

            }

        }

 

        /// <summary>

        /// Initializes the SmartDispatcher system, attempting to use the

        /// RootVisual of the plugin to retrieve a Dispatcher instance.

        /// </summary>

        public static void Initialize()

        {

            if (_instance == null)

            {

                RequireInstance();

            }

        }

 

        /// <summary>

        /// Initializes the SmartDispatcher system with the dispatcher

        /// instance.

        /// </summary>

        /// <param name="dispatcher">The dispatcher instance.</param>

        public static void Initialize(Dispatcher dispatcher)

        {

            if (dispatcher == null)

            {

                throw new ArgumentNullException("dispatcher");

            }

 

            _instance = dispatcher;

 

            if (_designer == null)

            {

                _designer = DesignerProperties.IsInDesignTool;

            }

        }

 

        /// <summary>

        /// 

        /// </summary>

        /// <returns></returns>

        public static bool CheckAccess()

        {

            if (_instance == null)

            {

                RequireInstance();

            }

 

            return _instance.CheckAccess();

        }

 

        /// <summary>

        /// Executes the specified delegate asynchronously on the user interface

        /// thread. If the current thread is the user interface thread, the

        /// dispatcher if not used and the operation happens immediately.

        /// </summary>

        /// <param name="a">A delegate to a method that takes no arguments and 

        /// does not return a value, which is either pushed onto the Dispatcher 

        /// event queue or immediately run, depending on the current thread.</param>

        public static void BeginInvoke(Action a)

        {

            if (_instance == null)

            {

                RequireInstance();

            }

 

            // If the current thread is the user interface thread, skip the

            // dispatcher and directly invoke the Action.

            if (_instance.CheckAccess() || _designer == true)

            {

                a();

            }

            else

            {

                _instance.BeginInvoke(a);

            }

        }

    }

}

SmartDispatcher 类的更多相关文章

  1. Java类的继承与多态特性-入门笔记

    相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ...

  2. C++ 可配置的类工厂

    项目中常用到工厂模式,工厂模式可以把创建对象的具体细节封装到Create函数中,减少重复代码,增强可读和可维护性.传统的工厂实现如下: class Widget { public: virtual i ...

  3. Android请求网络共通类——Hi_博客 Android App 开发笔记

    今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ...

  4. ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库

    在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ...

  5. ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core

    背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ...

  6. .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类

    .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ...

  7. Java基础Map接口+Collections工具类

    1.Map中我们主要讲两个接口 HashMap  与   LinkedHashMap (1)其中LinkedHashMap是有序的  怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ...

  8. PHP-解析验证码类--学习笔记

    1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1  定义变量 //随机因子 private $char ...

  9. C# 多种方式发送邮件(附帮助类)

    因项目业务需要,需要做一个发送邮件功能,查了下资料,整了整,汇总如下,亲测可用- QQ邮箱发送邮件 #region 发送邮箱 try { MailMessage mail = new MailMess ...

随机推荐

  1. HDU多校Round 5

    Solved:3 rank:71 E. Everything Has Changed #include <bits/stdc++.h> using namespace std; const ...

  2. 【原】Python学习

    1.常用模块介绍 #python -m SimpleHTTPServer 执行上面的命令就会在服务器当前目录下启动一个文件下载服务器,默认打开8000端口.这个时候,你只需要将IP和端口告诉客户端,即 ...

  3. 数组--P1980 计数问题

    题目描述 题解 试计算在区间 1 到 n的所有整数中,数字 x(0 ≤ x ≤ 9)共出现了多少次?例如,在 1到 11中,即在 1,2,3,4,5,6,7,8,9,10,11 中,数字 1 出现了 ...

  4. Codeforces 934D/933B - A Determined Cleanup

    传送门:http://codeforces.com/contest/934/problem/D 给定两个正整数p(p≥1).k(k>1).多项式f(x)的系数的取值集合为{0,1,2,...,k ...

  5. java使用JNA框架调用dll动态库

    这两天了解了一下java调用dll动态库的方法,总的有三种:JNI.JNA.JNative.其中JNA调用DLL是最方便的. ·JNI ·JNA ·JNative java使用 JNI来调用dll动态 ...

  6. 【codeforces 707E】Garlands

    [题目链接]:http://codeforces.com/contest/707/problem/E [题意] 给你一个n*m的方阵; 里面有k个联通块; 这k个联通块,每个连通块里面都是灯; 给你q ...

  7. 使用Mybatis进行连表查询、left join---https://blog.csdn.net/jinzhencs/article/details/51980518

    使用Mybatis进行连表查询.left join https://blog.csdn.net/jinzhencs/article/details/51980518

  8. ansible plugins简介

    ansible插件是增强ansible的核心功能的代码片段,ansible使用插件架构来实现丰富,灵活和可扩展的功能集. Ansible提供了许多方便的插件,您可以轻松编写自己的插件. 下边简单介绍A ...

  9. [luoguP2285] [HNOI2004]打鼹鼠(DP)

    传送门 设f[i]表示i个鼹鼠出现后,打死鼹鼠的最大值 动态转移方程:f[i]=max{f[j]+1}, 条件:abs(x[i]-x[j])+abs(y[i]-y[j])<=time[i]-ti ...

  10. zoj 3812 状压dp

    转载:http://blog.csdn.net/qian99/article/details/39138329 题意:给出n个物品,每个物品有两种属性Wi,Ti,有q组查询,每组查询要求在n个物品中选 ...