0% found this document useful (0 votes)
45 views2 pages

Using Using Using Using Using Using Using Using Using Using Using Using Using

This document contains code for a C# Windows Presentation Foundation (WPF) application that uses a BackgroundWorker to perform a long-running task. It defines handlers for the BackgroundWorker's DoWork, ProgressChanged, and RunWorkerCompleted events to update the UI with progress notifications. A button click handler starts and cancels the background task, updating labels to indicate running, canceled, or completed states.

Uploaded by

Talos Alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
45 views2 pages

Using Using Using Using Using Using Using Using Using Using Using Using Using

This document contains code for a C# Windows Presentation Foundation (WPF) application that uses a BackgroundWorker to perform a long-running task. It defines handlers for the BackgroundWorker's DoWork, ProgressChanged, and RunWorkerCompleted events to update the UI with progress notifications. A button click handler starts and cancels the background task, updating labels to indicate running, canceled, or completed states.

Uploaded by

Talos Alex
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

...extalos\source\repos\WpfApp1\WpfApp1\MainWindow.xaml.

cs 1
1 using System;
2 using System.Collections.Generic;
3 using System.Linq;
4 using System.Text;
5 using System.Threading.Tasks;
6 using System.Windows;
7 using System.Windows.Controls;
8 using System.Windows.Data;
9 using System.Windows.Documents;
10 using System.Windows.Input;
11 using System.Windows.Media;
12 using System.Windows.Media.Imaging;
13 using System.Windows.Navigation;
14 using System.Windows.Shapes;
15 using System.ComponentModel;
16
17
18 namespace WpfApp1
19 {
20 /// <summary>
21 /// Interaction logic for MainWindow.xaml
22 /// </summary>
23 public partial class MainWindow : Window
24 {
25 private readonly BackgroundWorker worker = new BackgroundWorker();
26 object state = new object[2];
27 string stateString;
28
29 public MainWindow()
30 {
31 InitializeComponent();
32 worker.DoWork += Worker_DoWork;
33 worker.RunWorkerCompleted += Worker_RunWorkerCompleted;
34 worker.ProgressChanged += Worker_ProgressChanged;
35 worker.WorkerSupportsCancellation = true;
36 worker.WorkerReportsProgress = true;
37 label1.Content = "Stopped";
38
39 }
40
41 private void Worker_ProgressChanged(object sender,
ProgressChangedEventArgs e)
42 {
43 progressBar1.Value = e.ProgressPercentage;
44 textBlock1.Text = ((e.UserState as object[])[1].ToString());
45 label1.Content = ((e.UserState as object[])[0]);
46 }
47
48 private void Worker_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
49 {
50 if (e.Cancelled == true)
51 {
52 label1.Content = "Canceled!";
53 }
54 else if (e.Error != null)
...extalos\source\repos\WpfApp1\WpfApp1\MainWindow.xaml.cs 2
55 {
56 label1.Content = "Error: " + e.Error.Message;
57 }
58 else
59 {
60 label1.Content = "Done!";
61 }
62
63 }
64
65 private void Worker_DoWork(object sender, DoWorkEventArgs e)
66 {
67 for (int i = 0; i <= 100; i++)
68 {
69 worker.ReportProgress(i,state);
70 for (int j = 0; j <= 500000; j++)
71 {
72 e.Result = j;
73
74 if (worker.CancellationPending)
75 {
76 e.Cancel = true;
77 state = new object[2] { stateString, j };
78 stateString = "Cancelation pending";
79 break;
80 }
81 else
82 {
83 stateString = "Running";
84 }
85 }
86 }
87 }
88
89 private void Button_Click(object sender, RoutedEventArgs e)
90 {
91 if (checkBox1.IsChecked == true)
92 {
93 checkBox1.IsChecked = false;
94 worker.CancelAsync();
95 }
96 else
97 {
98 checkBox1.IsChecked = true;
99 worker.RunWorkerAsync();
100 }
101 }
102 }
103 }
104

You might also like