- This topic explains about the working of threading using C# Backgroundworker control.
- To implement threading using C# BackgroundWorker, do the following:
1) Add two Button controls, a ListBox and a ProgressBar control to Form
2) Add a BackgroundWorker control to Form
3) Set its WorkerReportsProgress and WorkerSupportsCancellation properties to True
- Then finally add the code to the form to implement threading using C# BackgroundWorker
BackgroundWorker Class – Implement threading using C# BackgroundWorker
- BackgroundWorker class executes an operation on a separate thread.
- Syntax
|
1 2 |
[HostProtectionAttribute(SecurityAction.LinkDemand, SharedState = true)] public class BackgroundWorker : Component |
BackgroundWorker.RunWorkerAsync Method – Implement threading using C# BackgroundWorker
- BackgroundWorker.RunWorkerAsync Method helps to starts execution of a background operation.
BackgroundWorker.CancelAsync Method – Implement threading using C# BackgroundWorker
- BackgroundWorker.CancelAsync Method requests cancellation of a pending background operation.
BackgroundWorker.RunWorkerCompleted Event – Implement threading using C# BackgroundWorker
- BackgroundWorker.RunWorkerCompleted Event occurs when the background operation has completed, has been canceled, or has raised an exception.
Implement threading using C# BackgroundWorker – Complete Code
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace testc { public partial class Form1 : Form { private int mMin; private int mMax; private List<int> mResults = new List<int>(); public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { } private void btnstart_Click(object sender, EventArgs e) { listBox1.Items.Clear(); mMin = 1; mMax = 10000; backgroundWorker1.RunWorkerAsync(); } private void btnstop_Click(object sender, EventArgs e) { backgroundWorker1.CancelAsync(); } private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; } private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { foreach (int item in mResults) { listBox1.Items.Add(item); } } private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) { mResults.Clear(); for (int count = mMin; count <= mMax; count += 2) { bool isPrime = true; for (int x = 1; x <= Convert.ToInt32(count / 2); x++) { for (int y = 1; y <= x; y++) { if (x + y == count) { //the number is not prime isPrime = false; break; // TODO: might not be correct. Was : Exit For } } //short-circuit the check if (!isPrime) break; // TODO: might not be correct. Was : Exit For } if (isPrime) { mResults.Add(count); } this.backgroundWorker1.ReportProgress(Convert.ToInt32((count - mMin) / (mMax - mMin) * 100)); if (this.backgroundWorker1.CancellationPending) { return; } } } } } |
Implement threading using C# BackgroundWorker






