<Window x:Class="WpfApp53.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/20... ...
<Window x:Class="WpfApp53.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:WpfApp53" mc:Ignorable="d" Title="MainWindow" Height="450" Width="800"> <Grid> <Grid.RowDefinitions> <RowDefinition Height="*"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> <RowDefinition Height="Auto"/> </Grid.RowDefinitions> <ListBox Grid.Row="0" x:Name="listBox1"/> <ProgressBar Grid.Row="1" x:Name="progressBar1" Height="20" Margin="5"/> <StackPanel Grid.Row="2" Orientation="Horizontal" Margin="5"> <TextBlock Text="Status: "/> <TextBlock x:Name="statusTextBox"/> </StackPanel> <Grid Grid.Row="3" Margin="10"> <Grid.ColumnDefinitions> <ColumnDefinition Width="*"/> <ColumnDefinition Width="*"/> </Grid.ColumnDefinitions> <Button Grid.Column="0" x:Name="startBtn" Content="Start" Click="StartBtn_Click"/> <Button Grid.Column="1" x:Name="cancelBtn" Content="Cancel" Click="CancelBtn_Click"/> </Grid> </Grid> </Window>
1 using System; 2 using System.Collections.Generic; 3 using System.ComponentModel; 4 using System.Linq; 5 using System.Text; 6 using System.Threading; 7 using System.Threading.Tasks; 8 using System.Windows; 9 using System.Windows.Controls; 10 using System.Windows.Data; 11 using System.Windows.Documents; 12 using System.Windows.Input; 13 using System.Windows.Media; 14 using System.Windows.Media.Imaging; 15 using System.Windows.Navigation; 16 using System.Windows.Shapes; 17 18 namespace WpfApp53 19 { 20 /// <summary> 21 /// Interaction logic for MainWindow.xaml 22 /// </summary> 23 public partial class MainWindow : Window 24 { 25 BackgroundWorker bgWorker; 26 public MainWindow() 27 { 28 InitializeComponent(); 29 cancelBtn.IsEnabled = false; 30 } 31 32 private void StartBtn_Click(object sender, RoutedEventArgs e) 33 { 34 listBox1.Items.Clear(); 35 startBtn.IsEnabled = false; 36 cancelBtn.IsEnabled = true; 37 bgWorker = new BackgroundWorker(); 38 bgWorker.DoWork += BgWorker_DoWork; 39 bgWorker.ProgressChanged += BgWorker_ProgressChanged; 40 bgWorker.RunWorkerCompleted += BgWorker_RunWorkerCompleted; 41 bgWorker.WorkerReportsProgress = true; 42 bgWorker.WorkerSupportsCancellation = true; 43 44 int maxItems = 50; 45 progressBar1.Minimum = 1; 46 progressBar1.Maximum = 100; 47 48 statusTextBox.Text = "Starting..."; 49 bgWorker.RunWorkerAsync(maxItems); 50 } 51 52 private void BgWorker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) 53 { 54 if(e.Cancelled) 55 { 56 statusTextBox.Text = "Cancelled"; 57 } 58 else 59 { 60 statusTextBox.Text = "Completed"; 61 } 62 statusTextBox.IsEnabled = true; 63 cancelBtn.IsEnabled = false; 64 } 65 66 private void BgWorker_ProgressChanged(object sender, ProgressChangedEventArgs e) 67 { 68 double percent = (e.ProgressPercentage * 100) / 50; 69 progressBar1.Value = Math.Round(percent, 0); 70 listBox1.Items.Add(new ListBoxItem { Content = e.ProgressPercentage + " item added" }); 71 statusTextBox.Text = Math.Round(percent, 0) + "% percent completed"; 72 } 73 74 private void BgWorker_DoWork(object sender, DoWorkEventArgs e) 75 { 76 int? maxItems = e.Argument as int?; 77 for(int i=1;i<=maxItems.GetValueOrDefault();i++) 78 { 79 if(bgWorker.CancellationPending) 80 { 81 e.Cancel = true; 82 break; 83 } 84 Thread.Sleep(200); 85 bgWorker.ReportProgress(i); 86 } 87 } 88 89 private void CancelBtn_Click(object sender, RoutedEventArgs e) 90 { 91 bgWorker.CancelAsync(); 92 startBtn.IsEnabled = true; 93 } 94 } 95 }