Dispatcher 类提供用于管理线程工作项队列的服务。

效果演示:

<Window x:Class="WPF之Dispatcher对象.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="" Width="">
<Grid Background="BurlyWood" Height="" Width="">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="184*" />
<ColumnDefinition Width="172*" />
<ColumnDefinition Width="147*" />
</Grid.ColumnDefinitions> <Label Content="vlue:0" FontSize="" Height="" Margin="125,10,162,0" Name="label1" VerticalAlignment="Top" Grid.ColumnSpan="" />
<Button Content="Dispatcher 回调界面线程" Height="" Margin="125,0,117,157" Name="button1" VerticalAlignment="Bottom" Grid.ColumnSpan="" Click="button1_Click" />
<Button Content="直接更新UI界面线程" Height="" Margin="125,0,117,209" Name="button2" VerticalAlignment="Bottom" Grid.ColumnSpan="" Click="button2_Click" />
<Button Content="同步更新UI界面线程" Height="" Margin="125,0,117,105" Name="button3" VerticalAlignment="Bottom" Grid.ColumnSpan="" Click="button3_Click" />
<Button Content="异步更新UI界面线程" Height="" Margin="125,0,117,53" Name="button4" VerticalAlignment="Bottom" Grid.ColumnSpan="" Click="button4_Click" />
<ProgressBar Height="" Margin="125,97,0,0" Name="progressBar1" VerticalAlignment="Top" Grid.ColumnSpan="" HorizontalAlignment="Left" Width="" />
<Label Content="Dispatcher 应用" FontSize="" Height="" Margin="157,-142,135,0" Name="label3" VerticalAlignment="Top" Grid.ColumnSpan="" />
</Grid>
</Window>

后台简答代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Threading; namespace WPF之Dispatcher对象
{
/// <summary>
/// MainWindow.xaml 的交互逻辑
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
} private void button2_Click(object sender, RoutedEventArgs e)
{
for (int i = ; i <= ; i++)
{
this.label1.Content = "value:" + i+"%";
this.progressBar1.Value = i;
System.Threading.Thread.Sleep(); //不能引用 System.Windows.Forms中的DoEvents会降低性能
System.Windows.Forms.Application.DoEvents(); }
} private void button1_Click(object sender, RoutedEventArgs e)
{
for (int i = ; i <= ; i++)
{
this.label1.Content = "value:" + i+"%";
this.progressBar1.Value = i;
System.Threading.Thread.Sleep();
this.DoEvents();//调用扩展方法DoEvents刷新界面同步
}
}
public delegate void delegateUI(string val);
public void RefashUI(string val)
{
this.label1.Content = "value:" + val+"%";
this.progressBar1.Value = int.Parse(val); } private void button3_Click(object sender, RoutedEventArgs e)
{
for (int i = ; i <= ; i++)
{
this.Dispatcher.Invoke(new delegateUI(RefashUI), //同步执行
DispatcherPriority.Normal, //优先级设置
new string[] { i.ToString() });
System.Threading.Thread.Sleep();
this.DoEvents();
} }
private void button4_Click(object sender, RoutedEventArgs e)
{
for (int i = ; i <= ; i++)
{
this.Dispatcher.BeginInvoke(new delegateUI(RefashUI),//异步执行
DispatcherPriority.Normal,
new string[] { i.ToString() });
System.Threading.Thread.Sleep();
this.DoEvents(); //也可以用这种方法启动线程,设置线程优先级
//this.Dispatcher.Thread.Start();
//this.Dispatcher.Thread.Priority = System.Threading.ThreadPriority.Highest;
}
}
} //一个扩展的自定义DoEvents,可以直接使用我们的项目中
public static class WindowEX
{
public static void DoEvents(this MainWindow win)
{
DispatcherFrame frame = new DispatcherFrame();
Dispatcher.CurrentDispatcher.BeginInvoke(DispatcherPriority.Background,
new DispatcherOperationCallback(ExitFrames), frame);
Dispatcher.PushFrame(frame);
} public static object ExitFrames(object f)
{
((DispatcherFrame)f).Continue = false;
return null;
} }
}

上面展示了Dispatcher对象简单的运用,更多的用法http://msdn.microsoft.com/zh-cn/library/vstudio/System.Windows.Threading.Dispatcher.aspx

下载:http://files.cnblogs.com/BABLOVE/WPF%E4%B9%8BDispatcher%E5%AF%B9%E8%B1%A1.rar

WPFDispatcher示例的更多相关文章

  1. Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)

    本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...

  2. .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1

    微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...

  3. 通过Jexus 部署 dotnetcore版本MusicStore 示例程序

    ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...

  4. WCF学习之旅—第三个示例之四(三十)

           上接WCF学习之旅—第三个示例之一(二十七)               WCF学习之旅—第三个示例之二(二十八)              WCF学习之旅—第三个示例之三(二十九)   ...

  5. JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例

    一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...

  6. XAMARIN ANDROID 二维码扫描示例

    现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile  做一个简单的 Android 条码扫描示 ...

  7. iOS之ProtocolBuffer搭建和示例demo

    这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...

  8. Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)

    示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...

  9. oracle常用函数及示例

    学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...

随机推荐

  1. Quartz定时任务学习(九)Quartz监听器

    Quartz 提供了三种类型的监听器:监听 Job 的,监听 Trigger 的,和监听 Scheduler 自已的. 本章解释如何应用每一种类型来更好的管理你的 Quartz 应用,并获悉到什么事件 ...

  2. Linux和Windows互传文件命令(转)

    sftp>lls 显示当前目录内容 sftp>lcd g:\ 进入本地g盘 sftp>put phpMyAdmin.zip 将本地g盘下的phpMyAdmin.zip上传到/home ...

  3. LabVIEW中的UDP通信

    UDP(user datagram protoco1)提供向接收端发送信息的最简便的协议,与TCP不同,UDP不是面向连接的可靠数据流传输协议,而是面向操作的不可靠数据流传输协议.UDP在数据传输之前 ...

  4. Form 对象的集合

    <html> <body id="myBody" class="myBody"> <form id="myForm&qu ...

  5. JavaScript 开发规范要求详解

    作为一名开发人员(We前端JavaScript开发),不规范的开发不仅使日后代码维护变的困难,同时也不利于团队的合作,通常还会带来代码安全以及执行效率上的问题.本人在开发工作中就曾与不按规范来开发的同 ...

  6. 【Android】isEmpty()

    对字符串进行非空判断,可以一次性进行两种空值的判断.当传入的字符串等于null或者等于空字符串的时候这个方法都会返回true,从而不需要单独去判断这两种空值,再利用逻辑运算符连接起来.

  7. CentOS 6.7安装Tomcat 7

    1.下载Tomcat 7 wget http://apache.fayea.com/tomcat/tomcat-7/v7.0.67/bin/apache-tomcat-7.0.67.tar.gz 2. ...

  8. Linux下安装、配置、授权、调优Mysql

    以前在linux已经安装了很多次的Mysql,但是时间间隔长了以后,安装步骤总是会遗漏,趁这次安装,做一下安装记录. 检查系统是否已经安装Mysql rpm -qa|grep -i mysql Mys ...

  9. SVN状态图标不显示

    Windows最多只允许15个覆盖图标,它自己又用了几个,结果给用户用的就11个左右了,如果你安装了其他网盘,那可用的就更少了. 解决方法: 1.在运行里输入regedit进入注册表 2.依次打开HK ...

  10. compareTo()

    从字面意思可知这个方法就是比较的意思. 所以该方法有如下两种情况: 1.比较前后的两个字符不相同: (1)     String str = "Hello World"; Stri ...