WPFDispatcher示例

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

效果演示:WPFDispatcher示例

<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

上一篇:LearnMVC5-GettingStarted


下一篇:oracle创建存储过程并返回结果集(附C#调用代码)