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. Python学习之LeetCode刷题之路——简单题【1、7、9】

    1.两数之和 给定一个整数数组 nums 和一个目标值 target,请你在该数组中找出和为目标值的那 两个 整数,并返回他们的数组下标. 你可以假设每种输入只会对应一个答案.但是,你不能重复利用这个 ...

  2. 网络编程 - 简单的socket例子

    1.客户端 #客户端import socketclient=socket.socket() #生成socket连接对象client.connect(("localhost",696 ...

  3. C++11 Thread多线程的学习心得与问题

    C++11 ,封装了thread的多线程的类,这样对多线程的使用更加方便. 多线程的原理我不加赘述,可以参看操作系统等参考书. 多线程代码可以最大化利用计算机性能资源,提高代码的运行效率,是常用优化方 ...

  4. python使用xlrd和xlwt读写Excel文件

    版权声明:本文为博主原创文章,未经允许不得转载. 安装模块 如果使用的是Linux系统,并且安装了pip,可以直接使用pip安装xlrd, xlwt: pip install xlwt pip ins ...

  5. 阿里云安装nodejs

    cd进入root目录下: cd /root 下载node.js安装包 wget https://nodejs.org/dist/v8.11.2/node-v8.11.2-linux-x64.tar.x ...

  6. highcharts图表的常见操作

    <!DOCTYPE html><html lang="en"><head> <meta charset="UTF-8" ...

  7. L2-014. 列车调度(带图详解)

    L2-014. 列车调度   火车站的列车调度铁轨的结构如下图所示. Figure 两端分别是一条入口(Entrance)轨道和一条出口(Exit)轨道,它们之间有N条平行的轨道.每趟列车从入口可以选 ...

  8. 有关HTML的相关基础问题:

    有关HTML的相关基础问题:1.Doctype作用?严格模式与混杂模式如何区分?它们有何意义?   1)<!DICTYPE>声明位于文档中的最前面,处于<html>标签之前,告 ...

  9. type="application/javascript"

    type="application/javascript" html script 标签中 type有如下这些值,请问分别是什么意思,在什么情况下使用? type="te ...

  10. 数据库——mysql如何获取当前时间---https://www.cnblogs.com/Chenshuai7/p/5136469.html

    数据库——mysql如何获取当前时间 1.1 获得当前日期+时间(date + time)函数:now() -------https://www.cnblogs.com/Chenshuai7/p/51 ...