WPF中的几种处理线程的工作方式:
来源:互联网 发布:华为和高通的差距 知乎 编辑:程序博客网 时间:2024/05/18 03:30
WPF多线程演示
WPF中的几种处理线程的工作方式:
1.简单的DispatcherTimer类似Timer控件
2.需要处理UI同步时,Dispatcher DispatcherOpertion
3.增强的Thread对象 System.Windows.Threading
4.BackgroundWorker组建对象
下面看下展示着几种处理方式:xaml文件
<Window x:Class="WPF多线程演示.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="MainWindow" Height="350" Width="525" Background="BlanchedAlmond"> <Grid> <Label Content="UI多线程演示" FontSize="24" Height="49" Margin="139,28,205,0" Name="label1" VerticalAlignment="Top" /> <Button Content="DispatcherTimer" Height="37" Margin="103,73,129,0" Name="button1" VerticalAlignment="Top" Click="button1_Click" /> <Button Content="DispatcherThread" Height="35" Margin="103,116,129,0" Name="button2" VerticalAlignment="Top" Click="button2_Click" /> <Button Content="Thread" Margin="103,167,129,111" Name="button3" Click="button3_Click" /> <Button Content="BackgroundWorker" Height="40" Margin="103,0,129,65" Name="button4" VerticalAlignment="Bottom" Click="button4_Click" /> <ProgressBar Height="35" Margin="64,0,103,24" Name="progressBar1" VerticalAlignment="Bottom" /> <Label Height="35" HorizontalAlignment="Right" Margin="0,252,22,0" Name="label2" VerticalAlignment="Top" Width="60" /> </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多线程演示{ /// <summary> /// MainWindow.xaml 的交互逻辑 /// </summary> public partial class MainWindow : Window { public MainWindow() { InitializeComponent(); } //方式一 DispatcherTimer tm = new DispatcherTimer();//实例化一个DispatcherTimer对象 private void button1_Click(object sender, RoutedEventArgs e) { tm.Tick += new EventHandler(tm_Tick);//订阅Tick事件 tm.Interval = TimeSpan.FromSeconds(0.05); tm.Start(); // tm.Stop(); } void tm_Tick(object sender, EventArgs e) { if (progressBar1.Value<=100) { progressBar1.Value++; this.label2.Content = progressBar1.Value++ +"%"; } else { tm.Stop(); } } //方式二 public void newActionThread(int value) { this.progressBar1.Value = value; this.label2.Content = progressBar1.Value++ + "%"; System.Threading.Thread.Sleep(100); } // 使用线程 方法 public void DispatcherThread() { Dispatcher newDispatcher = Dispatcher.CurrentDispatcher;//提供线程工作环境 Action<int> newAction = new Action<int>(this.newActionThread); for (int i = 0; i < 100; i++) { newDispatcher.Invoke(newAction, i); System.Threading.Thread.Sleep(100); this.DoEvents(); // newDispatcher.Thread.Abort(); } } private void button2_Click(object sender, RoutedEventArgs e) { DispatcherThread(); } //方式三 public void newActionThread2(object value) { Action<int> newAction = new Action<int>(this.newActionThread); this.progressBar1.Dispatcher.Invoke(newAction, (int)value);//同步执行指定的委托 System.Threading.Thread.Sleep(100); } public void Thread() { //winform中的线程 // System.Threading.Thread; //wpf环境中的线程 //System.Windows.Threading.Dispatcher.CurrentDispatcher.Thread System.Threading.ParameterizedThreadStart ts = new System.Threading.ParameterizedThreadStart(this.newActionThread2);//一个线程执行委托 for (int i = 0; i <= 100; i++) { System.Threading.Thread t = new System.Threading.Thread(ts); System.Threading.Thread.Sleep(100); t.Start(i); this.label2.Content = progressBar1.Value++ + "%"; this.DoEvents();//界面刷新 } } private void button3_Click(object sender, RoutedEventArgs e) { Thread(); } //方式四BackgroundWorker //http://msdn.microsoft.com/zh-cn/library/vstudio/system.componentmodel.backgroundworker.aspx更多信息 System.ComponentModel.BackgroundWorker bw; private void button4_Click(object sender, RoutedEventArgs e) { bw = new System.ComponentModel.BackgroundWorker();//创建BackgroundWorker对象实例 bw.DoWork += new System.ComponentModel.DoWorkEventHandler(bw_DoWork);//订阅DoWork事件 bw.ProgressChanged += new System.ComponentModel.ProgressChangedEventHandler(bw_ProgressChanged);//订阅报告进程事件 bw.WorkerReportsProgress = true; bw.RunWorkerAsync();//开始执行后台操作 } void bw_ProgressChanged(object sender, System.ComponentModel.ProgressChangedEventArgs e) { this.progressBar1.Value = e.ProgressPercentage;//获取进度百分比 this.label2.Content = (e.ProgressPercentage.ToString() + "%"); } void bw_DoWork(object sender, System.ComponentModel.DoWorkEventArgs e) { for (int i = 0; i <= 100; i++) { this.bw.ReportProgress(i); System.Threading.Thread.Sleep(100); } } //方式5我们可以利用线程并行来处理 }}
类EX中的是一个扩展方法。进行界面刷新
public static class Ex { //扩展方法进行界面刷新 public static void DoEvents(this Window 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; } }
效果展示:
demo下载:http://files.cnblogs.com/BABLOVE/WPF%E5%A4%9A%E7%BA%BF%E7%A8%8B%E6%BC%94%E7%A4%BA.rar
0 0
- WPF中的几种处理线程的工作方式:
- ios 几种线程依赖的处理方式
- WPF中使用文件浏览对话框的几种方式
- WPF MVVM之INotifyPropertyChanged接口的几种实现方式
- WPF中使用文件浏览对话框的几种方式
- WPF中使用文件浏览对话框的几种方式
- 关于wpf设置属性的几种方式
- 线程同步的几种方式
- 让线程休息的几种方式
- 线程同步几种方式的比较
- 线程同步的几种方式
- 线程同步的几种方式
- 线程中断/恢复的几种方式
- C#几种创建线程的方式
- 总结几种创建线程的方式
- 线程同步控制的几种方式
- 线程等待的几种方式
- java线程同步的几种方式
- C++中“已经在 test.obj 中定义”的错误
- 整理GRE数学考试中正态分布考点
- 解决Android Studio的ADB not responding错误
- 自定义安卓控件错误 Custom view MySwitchBtn is not using the 2- or 3-argument View constructors; XML attribute
- 关于全志A20播放视频时透明控件变成黑色毛刺的问题
- WPF中的几种处理线程的工作方式:
- 掌握选项特点--提高SAT阅读
- Mongodb在Linux下的安装和启动和配置
- iOS时间前后获取
- Spring 解决编码问题-CharacterEncodingFilter
- 忽略时间,以'帧'为概念的重力系统(通过updatePosition来更新位置代替MoveTo)
- UML之用例图
- 权限项目总结(四) shiro 授权
- HTML5 video 跨浏览器兼容的方法