1. What's Background Worker?
Background worker same as the Thread, you can see it on Toolbox C# 4.0 (Microsoft studio 2012 above). This control can help you
- Makes threads easy to implement in Windows Forms.
- Intensive tasks need to be done on another thread so the UI does not freeze.
- It is necessary to post messages and update the user interface when the task is done.
Explore My Other Channel for More Cool and Valuable Insights
👉 Youtube Learn Tech Tips👉 Tiktok
👉 Facebook:2. How to use BackgroundWorker?
Background worker have 2 key properties:- WorkerReportsProgress
- WorkerSupportCancellation
Background worker have 3 key functions:
- DoWord: working
- ProgressChanged
- RunWorkerCompleted:When completed
GUI:
You can download full projects from here
Source code:
/*************************************************
* Developer: ViLH (Zidane) - huuvi168@gmail.com
* Lat Modified: 2015-02-25
* Demo run 100% done and open New form
*
* [Questions]
* Form doing task and gif image not work. Because Gif image running main Thread
* and task form running main Thread too. So the gif image not word.
*
* [Answer]
* This tutorial will be help you split two Thread:
* one is for main form another is for gif image.
*
* ***********************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Text;
using System.Windows.Forms;
namespace BackGroundWorker
{
public partial class Form1 : Form
{
private delegate void DelegateProgressbar();
public Form1()
{
InitializeComponent();
backgroundWorker1.WorkerReportsProgress = true;
backgroundWorker1.WorkerSupportsCancellation = true;
}
private void backgroundWorker1_RunWorkerCompleted(object sender,
RunWorkerCompletedEventArgs e)
{
if (e.Cancelled == true)
{
resultLabel.Text = "Canceled!";
}
else if (e.Error != null)
{
resultLabel.Text = "Error: " + e.Error.Message;
}
else
{
resultLabel.Text = "Done!";
// do something when finished
openForm();
this.Hide();
}
}
// Progress Changed
private void backgroundWorker1_ProgressChanged(object sender,
ProgressChangedEventArgs e)
{
resultLabel.Text = (e.ProgressPercentage.ToString() + "%");
}
// Start Thread
private void startAsyncButton_Click(object sender, EventArgs e)
{
if (backgroundWorker1.IsBusy != true)
backgroundWorker1.RunWorkerAsync();
}
// Cancel Thread
private void cancelAsyncButton_Click(object sender, EventArgs e)
{
if (backgroundWorker1.WorkerSupportsCancellation == true)
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
for (int i = 1; i <= 10; i++)
{
if (worker.CancellationPending == true)
{
e.Cancel = true;
break;
}
else
{
// Perform a time consuming operation and report progress.
System.Threading.Thread.Sleep(500);
worker.ReportProgress(i * 10);
}
}
}
// Because of BackgroundWorker is a Thread, so we should use
// InvokeRequired for open form
private void openForm()
{
if (InvokeRequired)
this.Invoke(new DelegateProgressbar(openForm), null);
else
{
Form2 frm = new Form2();
frm.Show();
}
}
}
}
You can download full projects from here
You can view MSDN from here
Thank you for reading this post. I hope you found it helpful and easy to follow. If you have any feedback or questions about
C# BackgroundWorker Tutorial ,
please share them in the comments below. I would love to hear from you and discuss this topic further
✋✋✋✋
Webzone Tech Tips, all things Tech Tips for web development
- I am Zidane, See you next time soon ✋✋✋✋