Background Worker
15:16Introduction
In this articales I want to show how to create a background worker in Dot Net.What is background worker?
A BackgroundWorker component to execute a time consuming process while main thread is still available to the user interface.Properties Of Background Worker
i) CancellationPending: This properties used to cancellation of a BackgroundWorker.ii) IsBusy: This Indicates if a BackgroundWorker is running an asynchronous operation.
iii) WorkerReportsProgress: This properties used to update progress report.
iv) WorkerSupportsCancellation: This properties indicates if a BackgroundWorker supports asynchronous cancellation.
Methods Of Background Worker
DoWork event is fired when the RunWorkerAsync method is called. This event fired our code continue when our application is still doing some other work.Methods Of Background Worker
There have two methods.i) RunWorkerAsync: It starts the thread.
ii) CancelAsync: It stop the thread.
Create Project
File > New > Project > Windows Forms Application > OKForm1.cs code is below:
Form1.cs
private BackgroundWorker b_Worker; private string status = ""; public Form1() { InitializeComponent(); b_Worker = new BackgroundWorker(); b_Worker.DoWork += new DoWorkEventHandler(b_Worker_DoWork); b_Worker.ProgressChanged += new ProgressChangedEventHandler(b_Worker_ProgressChanged); b_Worker.RunWorkerCompleted += new RunWorkerCompletedEventHandler(b_Worker_RunWorkerCompleted); b_Worker.WorkerReportsProgress = true; b_Worker.WorkerSupportsCancellation = true; } private void Form1_Load(object sender, EventArgs e) { } private void btnStart_Click(object sender, EventArgs e) { btnStart.Enabled = false; btnStop.Enabled = true; b_Worker.RunWorkerAsync(); } private void btnStop_Click(object sender, EventArgs e) { btnStart.Enabled = true; btnStop.Enabled = false; if (b_Worker.IsBusy) b_Worker.CancelAsync(); } void b_Worker_DoWork(object sender, DoWorkEventArgs e) { for (int i = 0; i <= 100; i++) { Thread.Sleep(100); if (i == 100) { Thread th = new Thread(new ThreadStart(WorkThread)); th.Start(); i = 0; } b_Worker.ReportProgress(i); if (b_Worker.CancellationPending) { e.Cancel = true; b_Worker.ReportProgress(0); return; } } b_Worker.ReportProgress(100); } private void WorkThread() { ShowStatus(); } void ShowStatus() { if (InvokeRequired) { MethodInvoker method = new MethodInvoker(ShowStatus); Invoke(method); return; } status += "Background process complete at " + DateTime.Now.ToString() + Environment.NewLine; textBox.Text = status; } void b_Worker_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e) { if (e.Cancelled) { lblStatus.Text = "Task Cancelled."; } else if (e.Error != null) { lblStatus.Text = "Error while performing background operation."; } else { lblStatus.Text = "Task Completed."; } btnStart.Enabled = true; btnStop.Enabled = false; } void b_Worker_ProgressChanged(object sender, ProgressChangedEventArgs e) { progressBar1.Value = e.ProgressPercentage; lblStatus.Text = "Processing......" + progressBar1.Value.ToString() + "%"; }Now run the application. The window will be show like this:
After click the start button the BackgroundWorker work like below:
If you want to stop then click stop button the window will show like below:
Here I create a new thread to show the work status. i.e.
Thread th = new Thread(new ThreadStart(WorkThread)); th.Start();
0 comments