Progress bar with background worker

This code sample shows how BackgroundWorker can be used for progress bar.

 

Progress bar of background worker

Progress bar form

public partial class ProgressForm : Form
{
    public Exception Error
    {
        get;
        private set;
    }

    private BackgroundWorker bw = new BackgroundWorker();

    public ProgressForm(DoWorkEventHandler doWorkEventHandler)
    {
        InitializeComponent();

        bw.WorkerReportsProgress = true;
        bw.WorkerSupportsCancellation = true;
        bw.DoWork += doWorkEventHandler;
        bw.ProgressChanged +=
            new ProgressChangedEventHandler(bw_ProgressChanged);
        bw.RunWorkerCompleted +=
            new RunWorkerCompletedEventHandler(bw_RunWorkerCompleted);
    }

    private void buttonCancel_Click(object sender, EventArgs e)
    {
        if (bw.WorkerSupportsCancellation == true)
        {
            bw.CancelAsync();
        }
    }

    private void ProgressForm_Shown(object sender, EventArgs e)
    {
        if (bw.IsBusy != true)
        {
            bw.RunWorkerAsync();
        }
    }

    #region Background worker

    private void bw_RunWorkerCompleted(object sender,
        RunWorkerCompletedEventArgs e)
    {
        if ((e.Cancelled == true))
        {
            this.DialogResult = System.Windows.Forms.DialogResult.Cancel;
        }
        else if (e.Error != null)
        {
            Error = e.Error;
            this.DialogResult = System.Windows.Forms.DialogResult.Abort;
        }
        else
        {
            this.DialogResult = System.Windows.Forms.DialogResult.OK;
        }
        this.Close();
    }

    private void bw_ProgressChanged(object sender, ProgressChangedEventArgs e)
    {
        int value = e.ProgressPercentage;
        this.Text = "Progress " + value + "% ...";
        this.progressBar1.Value = Math.Min(Math.Max(value, 0), 100);
    }

    #endregion Background worker
}

Background job

private void bw_DoWork(object sender, DoWorkEventArgs e)
{
    BackgroundWorker worker = sender as BackgroundWorker;
    for (int i = 1; i < 10; ++i)
    {
        // do the job
        System.Threading.Thread.Sleep(500);

        if (worker.CancellationPending == true)
        {
            e.Cancel = true;
            break;
        }
        worker.ReportProgress((i * 100)/n);
    }
}

Starting the background job and handling the result

private void button1_Click(object sender, EventArgs e)
{
    ProgressForm progressForm = new ProgressForm(new DoWorkEventHandler(bw_DoWork));
    progressForm.StartPosition = FormStartPosition.CenterParent;
    DialogResult result = progressForm.ShowDialog(this);
    switch (result)
    {
        case System.Windows.Forms.DialogResult.Cancel:
            MessageBox.Show("Canceled");
            break;
        case System.Windows.Forms.DialogResult.Abort:
            MessageBox.Show(progressForm.Error.ToString());
            break;
        case System.Windows.Forms.DialogResult.OK:
            MessageBox.Show("Done");
            break;
    }
}