当前位置: 首页 > 编程日记 > 正文

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);
            }
        }
    }
}

转载于:https://www.cnblogs.com/shiyix/p/3459066.html

相关文章:

三、临时弹出一个QQ对话窗口

第一种&#xff1a;需要添加好友才可以访问 <a href"http://wpa.qq.com/msgrd?v3&uin317985559&siteqq&menuyes" target"_blank">123 </a> 第二种&#xff1a;不需要添加好友即可访问 上网去搜吧&#xff0c;小臂崽子转载于:http…

ZJU-java进阶笔记 第七周(输入输出)

流是Java处理输入输出的方式流的基础类——以字节(byte)形式 InputStream OutputStream 例 public class Main {public static void main(String[] args){System.out.println("请输入&#xff1a;");//定义一个字节数组byte[] buffer new byte[1024];try {int len …

Jquery实现的Tabs页签

管理导航系统设置用户管理内容管理其他管理1111111222222222333333333334444444444555555555555555<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns&qu…

【组队学习】【35期】动手学数据分析

动手学数据分析 航路开辟者&#xff1a;陈安东、金娟娟、杨佳达、老表、李玲、张文涛、高立业领航员&#xff1a;六一航海士&#xff1a;郑园园、李牧轩、高岩 基本信息 开源内容&#xff1a;https://github.com/datawhalechina/hands-on-data-analysis开源内容&#xff1a;…

postfix邮箱服务

1、使用postfix服务之前&#xff0c;需要先安装DNS服务&#xff0c;指定邮件交换记录&#xff1b;2、编辑postfix服务配置文件&#xff1b;3、安装dovecot服务、用于本地进行邮件发接测试&#xff1b;4、创建两个用户&#xff0c;并添加到mailusers组中&#xff1b;5、在Linux虚…

mysql数据库基本介绍及常见使用

关系型数据库 ● 二维表 ● 表中的一行&#xff0c;称为记录&#xff0c;表中的列&#xff0c; 称为字段&#xff08;属性&#xff09; ● 行列交叉的单元格的值&#xff0c;叫属性值或字段值。 ● ORACLE DB2 Mysql SQL SERVER 关系型数据库 常用的指令(可以在命令行执行&…

FilenameFilter的使用

使用FilenameFilter过滤掉chapter开头的文件。 1 public class contentFilter implements FilenameFilter {2 public boolean isContent(String file) {3 if (file.startsWith("chapter")){4 return false;5 }else{6 return true;7 }8 …

Qt 串口连接

Qt 串口连接 使用 Qt 开发上位机程序时&#xff0c;经常需要用到串口&#xff0c;在 Qt 中访问串口比较简单&#xff0c;因为 Qt 已经提供了 QSerialPort 和 QSerialPortInfo 这两个类用于访问串口。 使用 QSerialPort Qt 提供的 QSerialPort 类继承于 QIODevice&#xff0c;也…

navicat for mysql如何在更新记录时自动记录更新时间

如图所示 步骤 添加属性recordTime(任意)设置属性类型为timestamps勾选 根据当前时间戳更新默认栏填 CURRENT_TIMESTAMP 效果

ASP.NET Web API自身对CORS的支持:从实例开始

在《通过扩展让ASP.NET Web API支持W3C的CORS规范》中我们通过自定义的HttpMessageHandler为ASP.NET Web API赋予了跨域资源共享的能力&#xff0c;具体来讲&#xff0c;这个自定义的CorsMessageHandler的自由主要体现在如下两个方面&#xff1a;其一&#xff0c;为简单跨域请求…

2021第12届蓝桥杯省赛 -- 填空题:试题B:直线

试题B&#xff1a;直线 问题描述 在平面直角坐标系中&#xff0c;两点可以确定一条直线。如果有多点在一条直线上&#xff0c; 那么这些点中任意两点确定的直线是同一条。 给定平面上 2 3 个整点(x,y)∣0≤x<2,0≤y<3,x∈Z,y∈Z{(x, y)|0 \leq x < 2, 0 \leq y &l…

Markdown快速上手

基本语法 标题 #标题名共六级&#xff0c;依次“#”数量增加&#xff0c;字体减小 加粗文字 哈喽 两个“* ”文字内容 再两个 斜体文字 哈喽 一个“*” 文字内容 再一个 无序列表 -列表内容回车两次退出此编辑模式 有序列表 1.列表内容回车两次退出此编辑模式 插入链接 百度链接…

复杂SELECT语句执行过程

通过FROM子句中找到需要查询的表通过WHERE子句进行分组函数筛选判断通过GROUP BY子句完成分组操作通过HAVING子句完成组函数筛选判断通过SELECT子句选择显示的列或表达式及组函数通过ORDER BY子句进行排序操作 书写时按照这个顺序:5 1 2 3 4 6 出处&#xff1a;东软Java实训

MTD NANDFLASH驱动相关知识介绍

转&#xff1a;http://blog.csdn.net/zhouzhuan2008/article/details/11053877 目录 MTD总概述MTD数据结构 MTD相关层实现MTD&#xff0c;Memory Technology Device即内存技术设备字符设备和块设备的区别在于前者只能被顺序读写&#xff0c;后者可以随机访问&#xff1b;同时&a…

Spring Boot轻松理解动态注入,删除bean

原文地址&#xff1a;http://412887952-qq-com.iteye.com/blog/2348445 ​ 我们通过getBean来获得对象,但这些对象都是事先定义好的,我们有时候要在程序中动态的加入对象.因为如果采用配置文件或者注解&#xff0c;我们要加入对象的话,还要重启服务,如果我们想要避免这一情况就…

mysql数据库常见进阶使用

事务 1&#xff09;mysql中的工作单元&#xff0c;由一个或者多个sql语句组成&#xff0c;“不成功便成仁”&#xff0c;要么全部执行成功&#xff0c;要么全部执行失败&#xff0c;以此来保证数据的一致性。 2&#xff09;事务的回滚&#xff1a;如果事务中的任何一个sql执行失…

[转]web打印实现方案 Lodop6.034 使用方法总结

本文转自&#xff1a;https://www.cnblogs.com/tiger8000/archive/2011/09/19/2181365.html 官文下载&#xff1a; http://mtsoftware.v053.gokao.net/download.html 本地 Lodop6.034 版本下载&#xff1a;/Files/tiger8000/Lodop6.034.rar 假设你的 lodop 打印控件放在你项目的…

图片的另一种展现—将后台图片编码直接展现为图片

1、应用场景 开发过程中&#xff0c;遇到这样的需求&#xff1a;需要将服务器上的图片展现在页面上&#xff0c;但是图片所在服务器不是对外的&#xff0c;图片所在服务器与应用服务器也不在同一台机器上&#xff0c;这时候就需要在开发中先将图片读出来&#xff0c;返回给应用…

电子学会青少年编程等级考试Python一级题目解析12

Python一级题目解析 1、题目&#xff08;2021.03&#xff09; 写一个计算长方形面积的程序&#xff0c;并对每行代码进行相应的注释&#xff0c;要求如下&#xff1a; &#xff08;1&#xff09;采用多行注释&#xff0c;说明程序的功能&#xff08;如下&#xff09;&#x…

Swing基础知识(更新中)

Swing是什么 做桌面应用程序的界面&#xff0c;GUI。 组件和容器&#xff1a;容器是特殊的组件。 布局管理器&#xff1a; 一般放中间容器&#xff0c;用来控制容器中组件的排列方式。 常见&#xff1a; ① FlowLayout 流布局(默认布局) 左上是起点&#xff0c;按组件加入容…

timesten 修改最大连接数

修改完/var/Timesten/sys.odbc.ini里面的connections之后 重启TT&#xff1a;ttdaemonadmin -restart 报错&#xff1a;15019: Only the instance admin may alter the Connections attribute Command> Command> connect dsn的名称;15019: Only the instance admin may a…

青少年编程竞赛交流群第050次活动录播

背景介绍 把电子学会的青少年编程能力等级测评作为游戏的关卡&#xff0c;带着小朋友们升级打怪&#xff0c;这个想法来自于 我从邵慧宁身上得到的启发。 升级打怪&#xff1a; 电子学会考评中心&#xff1a;http://www.qceit.org.cn/bos/default.html 知识内容&#xff1a…

JDBC连接mysql数据的7个步骤(讲解+源码)

步骤 源码 DBUtils类 package com.csu.db;import java.sql.*;public class DBUtils {public static Connection getConnection(){try {//[1/7] 加载JDBC的驱动Class.forName("com.mysql.cj.jdbc.Driver");//[2/7] 定义url连接参数String url "jdbc:mysql://l…

UML:概要设计,用什么画我的类图?

背景 做过需求之后&#xff0c;很少使用 UML 画概要设计&#xff0c;这几天尝试的用了几个工具&#xff0c;最总还是选择了 VisualStudio。 Edraw 详细信息很难编辑&#xff0c;如&#xff1a;签名。 Viso 添加成员太麻烦了。 VisualStudio 图形不支持着色。 备注 使用 VisualS…

RN Exception: Before building your project, you need to accept the license agreements and comp le...

异常 * What went wrong: A problem occurred configuring project :app. > You have not accepted the license agreements of the following SDK components: [Android SDK Platform 23, Android SDK Build-Tools 23.0.1]. Before building your project, you need to acc…

【组队学习】【35期】李宏毅机器学习(含深度学习)

李宏毅机器学习&#xff08;含深度学习&#xff09; 航路开辟者&#xff1a;王茂霖、陈安东&#xff0c;刘峥嵘&#xff0c;李玲领航员&#xff1a;梁家晖航海士&#xff1a;程浩伟、周小要、吴昌广 基本信息 开源内容&#xff1a;https://linklearner.com/datawhale-homepa…

git关键原理简介

集中化版本控制 缺点&#xff1a;1中央仓库得相当稳定&#xff0c;出问题可能每个人那里都没完整备份 2 只能在线使用&#xff08;今天网络已经不是问题&#xff09; 分布式版本控制 改进&#xff1a;每台客户机在本地都维护一份仓库 主要特点 保存数据和文件的主要方式 每个…

项目管理和缺陷跟踪工具Redmine

官网&#xff1a; http://www.redmine.org/ http://demo.redmine.org/ 下载&#xff1a; http://www.redmine.org/projects/redmine/wiki/Download Redmine 是一个开源的、基于Web的项目管理和缺陷跟踪工具。它用日历和甘特图辅助项目及进度可视化显示。同时它又支持多项目管理…