with this tutorials shows sample how to work in BackgroundWorker Control.
How to Build the Sample Project :
1.Create a new Visual C# Windows application project. Form1 is created by default.
2.Add a Button, ProgressBar,Lable control and BackgroundWorker Control to Form1.Size the form to be several inches wide by several inches tall.
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace CE.WinForms_backgroundWorker
{
public partial class Form1 : Form
{
// The progress form will be created and shown modally while the
// synchronous process is running. This form will notify the background
// thread if a cancellation is performed. The background thread
// will update the status label and ProgressBar
public Form1()
{
InitializeComponent();
//
this.lbprocessstatus.Visible = false;
}
///<summary>
/// this method will Enable Start procces Button ,
/// setting value 0 for ProgressBar
/// Changing visible status to false
///</summary>
private void reset()
{
this.btnstartprocess.Enabled = true;
this.progressBar1.Value = 0;
this.lbprocessstatus.Visible = false;
}
Boolean Delay()
{
if (this.backgroundWorker1.CancellationPending)
return false;
int a = 0;
while (a < 100 && !this.backgroundWorker1.CancellationPending)
{
//
// Working...
//
System.Threading.Thread.Sleep(100);
a++;
this.backgroundWorker1.ReportProgress(a);
}
return false;
}
///<summary>
/// this event Starts when the DoWork is called.
///</summary>
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
e.Result = Delay();
if (this.backgroundWorker1.CancellationPending)
e.Cancel = true;
}
///<summary>
/// this event Starts when the RunWorkerCompleted is called.
///</summary>
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
//
// Check to see if the background process was cancelled.
//
if (e.Cancelled)
{
System.Windows.Forms.MessageBox.Show("Processing cancelled.");
reset();
return;
}
if (e.Error!=null)
{
//
// Check to see if an error occurred in the
// background process.
//
System.Windows.Forms.MessageBox.Show(e.Error.Message);
return;
}
//
// Everything completed normally.
// process the response using e.Result
//
if ((bool)e.Result)
System.Windows.Forms.MessageBox.Show("True");
else
System.Windows.Forms.MessageBox.Show("Processing is complete.");
reset();
}
///<summary>
/// this event Starts when the ProgressChanged is called.
///</summary>
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
//
// Update the progressBar with the integer supplied to us from the
// ReportProgress() function.
//
this.progressBar1.Value = e.ProgressPercentage;
this.lbprocessstatus.Text = "Process Running " + this.progressBar1.Value;
}
///<summary>
/// this event Starts when the Start Procces Button is Clicked.
///</summary>
private void btnstartprocess_Click(object sender, EventArgs e)
{
this.btnstartprocess.Enabled = false;
this.lbprocessstatus.Visible = true;
this.backgroundWorker1.RunWorkerAsync();
}
///<summary>
/// this event fires when the Cancel Procces Button is Clicked.
///</summary>
private void btncancelprocess_Click(object sender, EventArgs e)
{
this.backgroundWorker1.CancelAsync();
}
}
}
APPLIES TO
Microsoft Visual C# .NET 2002 Standard Edition Microsoft Visual C# 2005 Express Edition Microsoft Visual C# 2008 Express Edition
Download sample below
This attachment is hidden for guests. Please log in or register to see it. This attachment is hidden for guests. Please log in or register to see it.