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. PrintWriter与ServletOutputStream的区别之文件下载

    copy自:https://blog.csdn.net/weixin_37703598/article/details/803870611.out = response.getWriter(); re ...

  2. The King’s Ups and Downs(HDU 4489,动态规划递推,组合数,国王的游戏)

    题意: 给一个数字n,让1到n的所有数都以波浪形排序,即任意两个相邻的数都是一高一低或者一低一高 比如:1324   4231,再比如4213就是错的,因为4高,2低,接下来1就应该比2高,但是它没有 ...

  3. 我的第一个随笔——MarkDown的简单用法!

    目录 我的第一个笔记 1. 学习简单的markdown语法 1.1 标题 1.2 引用 1.3 倾斜与加粗 1.4无序列表 1.5有序列表 1.6图片和网页 1.7分割线 1.8代码块 1.9结尾 2 ...

  4. zabbix部署-版本3.2.6

    172.18.237.14:一台主机上安装LAMP环境以及zabbix_server.zabbix_agentd 一.安装zibbix-server 1.环境要求 yum install mysql- ...

  5. swoft| 源码解读系列二: 启动阶段, swoft 都干了些啥?

    date: 2018-8-01 14:22:17title: swoft| 源码解读系列二: 启动阶段, swoft 都干了些啥?description: 阅读 sowft 框架源码, 了解 sowf ...

  6. npm 使用教程

    链接----------------------------------npm官网npm淘宝镜像 安装包----------------------------------npm install -g ...

  7. javaHttp请求,接收到的是中文乱码如何处理

    可在service()方法中加日志,看哪种不是乱码 例如,中文乱码的话,中文编码一般有 UTF-8,GBK,ISO-8859-1 加日志为 List<String> list = new ...

  8. 使用nfs3将hdfs挂载到本地或远程目录(非kerberos适用)

    最基本的配置方法,aix.kerberos等的操作详见http://hadoop.apache.org/docs/current/hadoop-project-dist/hadoop-hdfs/Hdf ...

  9. 【Codeforces 339C】Xenia and Weights

    [链接] 我是链接,点我呀:) [题意] 在天平上放砝码 你要在左边放一下然后到右边放一下 一直重复这样放m次 每次你放在其中一边都要让另外一边的重量比你少 你可以用1~10中的某些砝码 问你要怎样放 ...

  10. 团队开发git使用各种问题

    参考:https://www.cnblogs.com/schaepher/p/4933873.html 问题-3:保持github上项目干净,对于在不同机器上运行会不同的文件不予维护(如.idea/w ...