SmartDispatcher 类
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 类的更多相关文章
- Java类的继承与多态特性-入门笔记
		相信对于继承和多态的概念性我就不在怎么解释啦!不管你是.Net还是Java面向对象编程都是比不缺少一堂课~~Net如此Java亦也有同样的思想成分包含其中. 继承,多态,封装是Java面向对象的3大特 ... 
- C++ 可配置的类工厂
		项目中常用到工厂模式,工厂模式可以把创建对象的具体细节封装到Create函数中,减少重复代码,增强可读和可维护性.传统的工厂实现如下: class Widget { public: virtual i ... 
- Android请求网络共通类——Hi_博客 Android App 开发笔记
		今天 ,来分享一下 ,一个博客App的开发过程,以前也没开发过这种类型App 的经验,求大神们轻点喷. 首先我们要创建一个Andriod 项目 因为要从网络请求数据所以我们先来一个请求网络的共通类. ... 
- ASP.NET MVC with Entity Framework and CSS一书翻译系列文章之第二章:利用模型类创建视图、控制器和数据库
		在这一章中,我们将直接进入项目,并且为产品和分类添加一些基本的模型类.我们将在Entity Framework的代码优先模式下,利用这些模型类创建一个数据库.我们还将学习如何在代码中创建数据库上下文类 ... 
- ASP.NET Core 折腾笔记二:自己写个完整的Cache缓存类来支持.NET Core
		背景: 1:.NET Core 已经没System.Web,也木有了HttpRuntime.Cache,因此,该空间下Cache也木有了. 2:.NET Core 有新的Memory Cache提供, ... 
- .NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类
		.NET Core中间件的注册和管道的构建(2)---- 用UseMiddleware扩展方法注册中间件类 0x00 为什么要引入扩展方法 有的中间件功能比较简单,有的则比较复杂,并且依赖其它组件.除 ... 
- Java基础Map接口+Collections工具类
		1.Map中我们主要讲两个接口 HashMap 与 LinkedHashMap (1)其中LinkedHashMap是有序的 怎么存怎么取出来 我们讲一下Map的增删改查功能: /* * Ma ... 
- PHP-解析验证码类--学习笔记
		1.开始 在 网上看到使用PHP写的ValidateCode生成验证码码类,感觉不错,特拿来分析学习一下. 2.类图 3.验证码类部分代码 3.1 定义变量 //随机因子 private $char ... 
- C# 多种方式发送邮件(附帮助类)
		因项目业务需要,需要做一个发送邮件功能,查了下资料,整了整,汇总如下,亲测可用- QQ邮箱发送邮件 #region 发送邮箱 try { MailMessage mail = new MailMess ... 
随机推荐
- Ceres
			sudo apt-get install liblapack-dev libsuitesparse-dev libcxspares3.1.2 libgflags-dev libggoogle-glog ... 
- C++ Error C2662 cannot convert 'this' pointer from 'const *'
			---恢复内容开始--- 这个错误在于一点:常量对象只能调用常量成员(函数\变量),不能调用非常量成员.另一方面,非常量对象,既可以调用常量成员,又可以调用非常量成员. class A { publi ... 
- Python反射、异常处理
			反射 :字符串到对象属性的映射 hasattr(obj,string), 判断对象obj里面是否有叫string的字段或方法 getattr(obj,string) 获取obj对象里名叫string的 ... 
- CCF201612-2 工资计算 java(100分)
			试题编号: 201612-2 试题名称: 工资计算 时间限制: 1.0s 内存限制: 256.0MB 问题描述: 问题描述 小明的公司每个月给小明发工资,而小明拿到的工资为交完个人所得税之后的工资.假 ... 
- HDU - 6158 The Designer
			传送门:http://acm.hdu.edu.cn/showproblem.php?pid=6158 本题是一个计算几何题——四圆相切. 平面上的一对内切圆,半径分别为R和r.现在这一对内切圆之间,按 ... 
- Educational Codeforces Round 60 (Rated for Div. 2) 即Codeforces Round 1117 C题 Magic Ship
			time limit per test 2 second memory limit per test 256 megabytes input standard inputoutput standard ... 
- POJ 3270 置换群问题
			题目大意是: 每头牛都有一个对应的值a[i],现在给定一个初始的牛的序列,希望通过两两交换,能够使这些牛按值升序排列,每次交换都会耗费一个 a[i]+a[j] 希望耗费最小,求出这个最小耗费 个人觉得 ... 
- hdu  3804树链剖分+离线操作
			/* 树链刨分+离线操作 题意:给你一棵树,和询问x,y 从节点x--节点1的小于等于y的最大值. 解:先建一个空树,将树的边权值从小到大排序,将询问y按从小到大排序 对于每次询问y将小于等于y的边权 ... 
- poj  2823  二分法+单调队列
			#include<stdio.h> #include<string.h> #define N 1100000 int a[N]; int fmin[N],fmax[N]; i ... 
- - > 最大公约数(辗转相除法)和最小公倍数(公式法)
			最大公约数 #include<iostream> using namespace std; int a,b; int gcd(int x,int y){ return x==0?y:gcd ... 
