WPFDispatcher示例
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示例的更多相关文章
- Swift3.0服务端开发(一) 完整示例概述及Perfect环境搭建与配置(服务端+iOS端)
本篇博客算是一个开头,接下来会持续更新使用Swift3.0开发服务端相关的博客.当然,我们使用目前使用Swift开发服务端较为成熟的框架Perfect来实现.Perfect框架是加拿大一个创业团队开发 ...
- .NET跨平台之旅:将示例站点升级至 ASP.NET Core 1.1
微软今天在 Connect(); // 2016 上发布了 .NET Core 1.1 ,ASP.NET Core 1.1 以及 Entity Framework Core 1.1.紧跟这次发布,我们 ...
- 通过Jexus 部署 dotnetcore版本MusicStore 示例程序
ASPNET Music Store application 是一个展示最新的.NET 平台(包括.NET Core/Mono等)上使用MVC 和Entity Framework的示例程序,本文将展示 ...
- WCF学习之旅—第三个示例之四(三十)
上接WCF学习之旅—第三个示例之一(二十七) WCF学习之旅—第三个示例之二(二十八) WCF学习之旅—第三个示例之三(二十九) ...
- JavaScript学习笔记(一)——延迟对象、跨域、模板引擎、弹出层、AJAX示例
一.AJAX示例 AJAX全称为“Asynchronous JavaScript And XML”(异步JavaScript和XML) 是指一种创建交互式网页应用的开发技术.改善用户体验,实现无刷新效 ...
- XAMARIN ANDROID 二维码扫描示例
现在二维码的应用越来越普及,二维码扫描也成为手机应用程序的必备功能了.本文将基于 Xamarin.Android 平台使用 ZXing.Net.Mobile 做一个简单的 Android 条码扫描示 ...
- iOS之ProtocolBuffer搭建和示例demo
这次搭建iOS的ProtocolBuffer编译器和把*.proto源文件编译成*.pbobjc.h 和 *.pbobjc.m文件时,碰到不少问题! 搭建pb编译器到时没有什么问题,只是在把*.pro ...
- Android种使用Notification实现通知管理以及自定义通知栏(Notification示例四)
示例一:实现通知栏管理 当针对相同类型的事件多次发出通知,作为开发者,应该避免使用全新的通知,这时就应该考虑更新之前通知栏的一些值来达到提醒用户的目的.例如我们手机的短信系统,当不断有新消息传来时,我 ...
- oracle常用函数及示例
学习oracle也有一段时间了,发现oracle中的函数好多,对于做后台的程序猿来说,大把大把的时间还要学习很多其他的新东西,再把这些函数也都记住是不太现实的,所以总结了一下oracle中的一些常用函 ...
随机推荐
- IO(Input Output)流__字符流
一.IO流简述------用于操作数据的 IO流用来处理设备之间的数据传输; Java对数据的操作是通过流的方式; Java用于操作流的对象都是在IO包中; 流按操作数据分为: 字节流(通用)和字符流 ...
- Java类加载机制深度分析
转自:http://my.oschina.net/xianggao/blog/70826 参考:http://www.ibm.com/developerworks/cn/java/j-lo-class ...
- 【转】Android Camera(五)使用Camera功能 AREA的理解
http://blog.csdn.net/think_soft/article/details/7998478 使用Camera功能 大多数的Camera功能都是使用Camera.Parameters ...
- oracle4
分页查询 按雇员的id号升序取出 oracle的分页一共有三种方式 .根据rowid来分 ) ) order by cid desc; 执行时间0.03秒 .按分析函数来分 and rk> ...
- HDU2054JAVA
(包括2016级新生)除了校赛,还有什么途径可以申请加入ACM校队? A == B ? Time Limit: 1000/1000 MS (Java/Others) Memory Limit: ...
- hdu2041java
超级楼梯 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submis ...
- VOL.1 利用vmware ThinApp 制作非XP下可以运行的IE6 【无插件版】(windows vista/7/8 x86/x64 )
作为一名前端开发工程师,不免要考虑IE6的兼容性,但是大部分挑剔的同行们估计都不会用XP,所以基本上IE6的兼容性测试,都是使用IE Tester或者虚拟机. IE Tester的话,很多地方模拟的还 ...
- char nvarchar varchar
1. char 固定长度,最长n个字符. 2. varchar 最大长度为n的可变字符串. (n为某一整数,不同数据库,最大长度n不同) char和varchar区别: ...
- width:100% 和 max-width:100%; 有区别吗【转藏】
这个博客是基于“Pelican+Markdown+定制的my-gum主题”的.定制的主题将博文正文页面的 右边栏去掉,这导致在Firefox等浏览器中,正文中大的图片会突破正文块的宽度,高度也得不到限 ...
- [转]eclipse github 提交代码
1 git add2 git commit3 git pull (会产生冲突) 分成自动合并和手动合并4 处理冲突的文件 5 git push 本次commit 我用的是Eclipse的插件EGit ...