转自:http://blog.csdn.net/webkis/article/details/5977399
1 <Window x:Class="MyWPF.MainWindow" 2 xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 3 xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 4 Title="DownLoad Demo" Height="350" Width="525" Loaded="Window_Loaded"> 5 <Window.Background> 6 <LinearGradientBrush> 7 <GradientStop Color="Red" Offset="0"></GradientStop> 8 <GradientStop Color="Yellow" Offset="0.5"></GradientStop> 9 </LinearGradientBrush> 10 </Window.Background> 11 <Grid> 12 <Label x:Name="txtWords"></Label> 13 <StackPanel Orientation="Horizontal" HorizontalAlignment="Right" VerticalAlignment="Bottom"> 14 <Button Content="start download" Width="120" Height="23" Margin="8" Click="Button_Click"> 15 <Button.Effect> 16 <DropShadowEffect Color="Blue" BlurRadius="1"></DropShadowEffect> 17 </Button.Effect> 18 </Button> 19 <Button Content="cancel" Width="70" Height="23" Margin="8" Click="Button_Click_1"> 20 <Button.Effect> 21 <DropShadowEffect Color="Blue" BlurRadius="1"></DropShadowEffect> 22 </Button.Effect> 23 </Button> 24 </StackPanel> 25 <Rectangle Width="220" Stroke="Red" StrokeThickness="2" Height="60" RadiusX="5" RadiusY="5"> 26 </Rectangle> 27 <ProgressBar x:Name="progressBar" Width="180" Height="23" /> 28 </Grid> 29 </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.ComponentModel; namespace MyWPF { /// <summary> /// Interaction logic for MainWindow.xaml /// </summary> public partial class MainWindow : Window { BackgroundWorker worker; public MainWindow() { InitializeComponent(); } private void Window_Loaded(object sender, RoutedEventArgs e) { txtWords.Content = "download demo."; } private void Button_Click(object sender, RoutedEventArgs e) { worker = new BackgroundWorker(); worker.WorkerReportsProgress = true; worker.WorkerSupportsCancellation = true; worker.DoWork += new DoWorkEventHandler(worker_DoWork); worker.ProgressChanged += new ProgressChangedEventHandler(worker_ProgressChanged); worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(worker_RunWorkerCompleted); progressBar.Value = 0; txtWords.Content = "downloading..."; worker.RunWorkerAsync(); } void worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { Action<string> act = delegate(string str) { txtWords.Content = str; }; if (e.Cancelled) this.Dispatcher.BeginInvoke(act, "download cancelled."); else this.Dispatcher.BeginInvoke(act, e.Result.ToString()); } private static string Download(string url, string name, object sender,DoWorkEventArgs e) { BackgroundWorker w = sender as BackgroundWorker; for (int i = 0; i < 100; i++) { System.Threading.Thread.Sleep(50); w.ReportProgress(i + 1); if (w.CancellationPending) { e.Cancel = true; return ""; } } return "download completed, download " + DateTime.Now.Millisecond + "byte"; } void worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar.Value = e.ProgressPercentage; } void worker_DoWork(object sender, DoWorkEventArgs e) { e.Result = Download("", "", sender, e); } private void Button_Click_1(object sender, RoutedEventArgs e) { worker.CancelAsync(); txtWords.Content = "download demo."; progressBar.Value = 0; } } }
在后来http://developer.51cto.com/art/200912/173330.htm的文章中看到如下方法很好的解决了我自己的实际问题.
double value=0; for (.....) { progressBar.Dispatcher.Invoke(new Action<System.Windows.DependencyProperty, object>(progressBar.SetValue), System.Windows.Threading.DispatcherPriority.Background, ProgressBar.ValueProperty, value); value++; }