【WPF】The example of progress bar, label status changed by background worker
来源:互联网 发布:淘宝太坑图片 编辑:程序博客网 时间:2024/06/07 18:28
Demo screenshot;
xaml source code:
<Window x:Class="CommonTesting.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Title="ProcessDisplay" Height="350" Width="525"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> <RowDefinition Height="*" /> </Grid.RowDefinitions> <Label Grid.Row="0" Name="lblDisplay" /> <ProgressBar Grid.Row="1" Name="pgbPrinting" /> <Button Grid.Row="2" Name="btnPrint" Content="Print" Click="btnPrint_Click" /> <Button Grid.Row="3" Name="btnPause" Content="Pause" IsEnabled="False" Click="btnPause_Click" /> <Button Grid.Row="4" Name="btnCancel" Content="Cancel" IsEnabled="False" Click="btnCancel_Click" /> </Grid></Window>
cs source code:
using System;using System.ComponentModel;using System.Threading;using System.Windows;namespace CommonTesting{ /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { private BackgroundWorker _worker = new BackgroundWorker(); private ManualResetEvent _manualReset = new ManualResetEvent(true); private bool _isPause = false; public MainWindow() { InitializeComponent(); _worker.DoWork += new DoWorkEventHandler(backgroundWorker_DoWork); _worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(backgroundWorker_RunWorkerCompleted); _worker.WorkerSupportsCancellation = true; //for progress bar _worker.WorkerReportsProgress = true; _worker.ProgressChanged += new ProgressChangedEventHandler(backgroundWorker_ProgressChanged); } void backgroundWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) { pgbPrinting.Value = e.ProgressPercentage; } void backgroundWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { this.Dispatcher.Invoke(new Action(() => { lblDisplay.Content = "Cancel..."; pgbPrinting.Value = 0; })); } else if (e.Error != null) { this.Dispatcher.Invoke(new Action(() => { lblDisplay.Content = "Error..."; pgbPrinting.Value = 0; })); } else { this.Dispatcher.Invoke(new Action(() => { lblDisplay.Content = "Completed..."; })); } btnPrint.IsEnabled = true; } void backgroundWorker_DoWork(object sender, DoWorkEventArgs e) { this.Dispatcher.Invoke(new Action(() => { lblDisplay.Content = "Start..."; })); for (int i = 0; i < 100; i++) { Thread.Sleep(100); if (_worker.CancellationPending) { //receive cancellation message e.Cancel = true; return; } _manualReset.WaitOne(); _worker.ReportProgress(i + 1); } } private void btnPrint_Click(object sender, RoutedEventArgs e) { btnPrint.IsEnabled = false; btnCancel.IsEnabled = btnPause.IsEnabled = true; _worker.RunWorkerAsync(); } private void btnCancel_Click(object sender, RoutedEventArgs e) { btnPrint.IsEnabled = true; btnCancel.IsEnabled = btnPause.IsEnabled = false; if (_isPause) { PauseToggle(); } _worker.CancelAsync(); } private void btnPause_Click(object sender, RoutedEventArgs e) { PauseToggle(); } private void PauseToggle() { _isPause = !_isPause; if (_isPause) { _manualReset.Reset(); btnPause.Content = "Continue"; lblDisplay.Content = "Pause..."; } else { _manualReset.Set(); btnPause.Content = "Pause"; lblDisplay.Content = "Start..."; } } }}
0 0
- 【WPF】The example of progress bar, label status changed by background worker
- the progress bar
- IOS7 The Status Bar
- Hiding the Status Bar
- The Background of Background
- Showing progress bar in a status bar pane
- Showing progress bar in a status bar pane
- A Wyckoff Bar-by-Bar Analysis Example
- Android navigation bar and the status bar
- progress bar
- S 7 - Status bar overlaps the view
- Example of Remoting by Hessian
- iOS 【错误:'Changing the delegate of a tab bar managed by a tab bar controller is not allowed.'】
- progress 中的break by 以及first-of
- The RDD API By Example
- The RDD API By Example
- Status bar
- 管理系统UI之二:隐藏Status Bar (Hiding the Status Bar)
- 降火,胃火,肝火等
- 嗟夫!范文正公,真豁出生命之意义也
- 用Android NDK编译FFmpeg
- duilib进阶教程 -- 改进List控件_优化支持多选拉伸 (2)
- Linux中的虚拟化
- 【WPF】The example of progress bar, label status changed by background worker
- DataTable如何删除特定行
- 37wan《魔龙诀》首创套装收集 新年战力新飙升
- webservice之rmi
- mysql中having和where的区别
- java普通类在任意位置得到spring 中的bean
- [转]Linux内核的文件预读(readahead)
- 我也说说bogomips
- mysql索引类型normal,unique,full text