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. tomcat多域名配置

    1.找到你的tomcat然后在conf文件中找到server.xml打开: <Server port="8005" shutdown="SHUTDOWN" ...

  2. Hibernate查询之Example查询

    org.hibernate.criterion.Example 类允许你通过一个给定实例构建一个条件查询. 此实例的属性值将做成查询条件. Cat cat = new Cat(); cat.setSe ...

  3. Look and say numbers

    地址:http://www.codewars.com/kata/53ea07c9247bc3fcaa00084d/train/python There exists a sequence of num ...

  4. 03 将MDB文件在DATAGRID中显示

    附件:http://files.cnblogs.com/xe2011/MDB_BindingSource.rar using System; using System.Collections.Gene ...

  5. 苹果电脑出现 .DS_Store文件,删除不掉怎么处理?

    今天早上打开电脑,发现桌面上出现一个.DS_Store文件.可是出现的莫名其妙,也就没在意,可是重复删除之后还是出现.后来查了一下网上的资料,才知道,昨天我加入git的时候,为了看见 .git这个隐藏 ...

  6. android 60 发送短信

    import android.os.Bundle; import android.app.Activity; import android.telephony.SmsManager; import a ...

  7. 如何设计一个更好的C++ ORM

    2016/11/26 "用C++的方式读写数据库,简直太棒了!" 上一篇相关文章:如何设计一个简单的C++ ORM (旧版代码)

  8. 如何让android sdk manager飞奔安装sdk

    由于国内的gwf的原因,凡是google的服务都是无法正常访问,这两天从android官网上下载sdk不是一般的慢.不过现在有一个好的方法.sdk都是从https://dl-ssl.google.co ...

  9. Effective C++ 笔记二 构造/析构/赋值运算

    条款05:了解C++默默编写并调用哪些函数 编译器默认声明一个default构造函数.一个copy构造函数.一个copy assignment操作符和一个析构函数.这些函数都是public且inlin ...

  10. css hack方法总结

    IE6识别:下划线 _ 和 星号 *IE7识别:星号 *IE6-IE10识别: \9 css hack方式,条件注释法<!--[if IE]>这段文字只在IE浏览器显示<![endi ...