zoukankan      html  css  js  c++  java
  • 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="427" Width="576">
        <Grid Background="BurlyWood" Height="399" Width="557">
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="184*" />
                <ColumnDefinition Width="172*" />
                <ColumnDefinition Width="147*" />
            </Grid.ColumnDefinitions>
         
            <Label Content="vlue:0" FontSize="30" Height="55" Margin="125,10,162,0" Name="label1" VerticalAlignment="Top" Grid.ColumnSpan="3" />
            <Button Content="Dispatcher 回调界面线程" Height="46" Margin="125,0,117,157" Name="button1" VerticalAlignment="Bottom" Grid.ColumnSpan="3" Click="button1_Click" />
            <Button Content="直接更新UI界面线程" Height="46" Margin="125,0,117,209" Name="button2" VerticalAlignment="Bottom" Grid.ColumnSpan="3" Click="button2_Click" />
            <Button Content="同步更新UI界面线程" Height="46" Margin="125,0,117,105" Name="button3" VerticalAlignment="Bottom" Grid.ColumnSpan="3" Click="button3_Click" />
            <Button Content="异步更新UI界面线程" Height="46" Margin="125,0,117,53" Name="button4" VerticalAlignment="Bottom" Grid.ColumnSpan="3" Click="button4_Click" />
            <ProgressBar Height="32" Margin="125,97,0,0" Name="progressBar1" VerticalAlignment="Top" Grid.ColumnSpan="3" HorizontalAlignment="Left" Width="315" />
            <Label Content="Dispatcher 应用" FontSize="24" Height="41" Margin="157,-142,135,0" Name="label3" VerticalAlignment="Top" Grid.ColumnSpan="3" />
        </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 = 0; i <= 100; i++)
                {
                    this.label1.Content = "value:" + i+"%";
                    this.progressBar1.Value = i;
                    System.Threading.Thread.Sleep(100);
    
                    //不能引用  System.Windows.Forms中的DoEvents会降低性能
                    System.Windows.Forms.Application.DoEvents();
                   
                }
            }
    
            private void button1_Click(object sender, RoutedEventArgs e)
            {
                for (int i = 0; i <= 100; i++)
                {
                    this.label1.Content = "value:" + i+"%";
                    this.progressBar1.Value = i;
                    System.Threading.Thread.Sleep(100);
                    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 = 0; i <= 100; i++)
                {
                    this.Dispatcher.Invoke(new delegateUI(RefashUI), //同步执行
                        DispatcherPriority.Normal, //优先级设置
                        new string[] { i.ToString() });
                    System.Threading.Thread.Sleep(100);
                    this.DoEvents();
                }
    
            }
            private void button4_Click(object sender, RoutedEventArgs e)
            {
                for (int i = 0; i <= 100; i++)
                {
                    this.Dispatcher.BeginInvoke(new delegateUI(RefashUI),//异步执行
                        DispatcherPriority.Normal,
                        new string[] { i.ToString() });
                    System.Threading.Thread.Sleep(100);
                    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

  • 相关阅读:
    IIS6.0应用程序池回收和工作进程
    SQL Server 中的 bit 类型的使用
    VS2008 SP1简体中文版下载与.Net Framework 3.5 SP1简体中文版下载
    C#技术教程:http://www.cftea.com/docs/asp.net/csharp/
    C#中得到程序当前工作目录和执行目录的一些方法
    C#中 ? 运算符 和 ??运算符
    dhl:页面中调用 通过类返回SqlDataReader类型的对象
    C# 枚举(enum)-设计
    CnBlogs博文排版技巧 写博文了~~
    金山快译2007版
  • 原文地址:https://www.cnblogs.com/BABLOVE/p/3236053.html
Copyright © 2011-2022 走看看