using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace Wunnell.Demo.BackgroundWorkerForm.CS
{
public partial class BackgroundWorkerForm : Form
{
#region Fields
private readonly DoWorkEventHandler onDoWork;
private readonly ProgressChangedEventHandler onProgressChanged;
private readonly RunWorkerCompletedEventHandler onRunWorkerCompleted;
#endregion Fields
#region Constructors
/// <summary>
/// Creates a new instance of the <see cref="BackgroundWorkerForm"/> class.
/// </summary>
/// <remarks>
/// Parameterless constructor is private to ensure handlers are provided for <see cref="BackgroundWorker"/>.
/// </remarks>
public BackgroundWorkerForm()
{
InitializeComponent();
}
/// <summary>
/// Creates a new instance of the <see cref="BackgroundWorkerForm" /> class.
/// </summary>
/// <param name="onDoWork">
/// Handler for the <see cref="BackgroundWorker.DoWork">RunWorkerCompleted</see> event of a <see cref="BackgroundWorker"/>.
/// </param>
public BackgroundWorkerForm(DoWorkEventHandler onDoWork)
: this()
{
this.onDoWork = onDoWork;
// AddHandler is used for local event handlers so that remote event handlers can be registered first and thus executed first.
// Remote event handlers
backgroundWorker1.DoWork += onDoWork;
// Local event handlers
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
}
/// <summary>
/// Creates a new instance of the <see cref="BackgroundWorkerForm" /> class.
/// </summary>
/// <param name="onDoWork">
/// Handler for the <see cref="BackgroundWorker.DoWork">RunWorkerCompleted</see> event of a <see cref="BackgroundWorker"/>.
/// </param>
/// <param name="onProgressChanged">
/// Handler for the <see cref="BackgroundWorker.ProgressChanged">RunWorkerCompleted</see> event of a <see cref="BackgroundWorker"/>.
/// </param>
public BackgroundWorkerForm(DoWorkEventHandler onDoWork, ProgressChangedEventHandler onProgressChanged)
: this()
{
this.onDoWork = onDoWork;
this.onProgressChanged = onProgressChanged;
// AddHandler is used for local event handlers so that remote event handlers can be registered first and thus executed first.
// Remote event handlers
backgroundWorker1.DoWork += onDoWork;
backgroundWorker1.ProgressChanged += onProgressChanged;
// Local event handlers
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
// A ProgressChanged handler has been provided so the ProgressBar will be updated explicitly based on the BackgroundWorker.
backgroundWorker1.WorkerReportsProgress = true;
progressBar1.Style = ProgressBarStyle.Continuous;
}
/// <summary>
/// Creates a new instance of the <see cref="BackgroundWorkerForm" /> class.
/// </summary>
/// <param name="onDoWork">
/// Handler for the <see cref="BackgroundWorker.DoWork">RunWorkerCompleted</see> event of a <see cref="BackgroundWorker"/>.
/// </param>
/// <param name="onRunWorkerCompleted">
/// Handler for the <see cref="BackgroundWorker.RunWorkerCompleted">RunWorkerCompleted</see> event of a <see cref="BackgroundWorker"/>.
/// </param>
public BackgroundWorkerForm(DoWorkEventHandler onDoWork, RunWorkerCompletedEventHandler onRunWorkerCompleted)
: this()
{
this.onDoWork = onDoWork;
this.onRunWorkerCompleted = onRunWorkerCompleted;
// AddHandler is used for local event handlers so that remote event handlers can be registered first and thus executed first.
// Remote event handlers
backgroundWorker1.DoWork += onDoWork;
backgroundWorker1.RunWorkerCompleted += onRunWorkerCompleted;
// Local event handlers
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
}
/// <summary>
/// Creates a new instance of the <see cref="BackgroundWorkerForm" /> class.
/// </summary>
/// <param name="onDoWork">
/// Handler for the <see cref="BackgroundWorker.DoWork">RunWorkerCompleted</see> event of a <see cref="BackgroundWorker"/>.
/// </param>
/// <param name="onProgressChanged">
/// Handler for the <see cref="BackgroundWorker.ProgressChanged">RunWorkerCompleted</see> event of a <see cref="BackgroundWorker"/>.
/// </param>
/// <param name="onRunWorkerCompleted">
/// Handler for the <see cref="BackgroundWorker.RunWorkerCompleted">RunWorkerCompleted</see> event of a <see cref="BackgroundWorker"/>.
/// </param>
public BackgroundWorkerForm(
DoWorkEventHandler onDoWork,
ProgressChangedEventHandler onProgressChanged,
RunWorkerCompletedEventHandler onRunWorkerCompleted)
: this()
{
this.onDoWork = onDoWork;
this.onProgressChanged = onProgressChanged;
this.onRunWorkerCompleted = onRunWorkerCompleted;
// AddHandler is used for local event handlers so that remote event handlers can be registered first and thus executed first.
// Remote event handlers
backgroundWorker1.DoWork += onDoWork;
backgroundWorker1.ProgressChanged += onProgressChanged;
backgroundWorker1.RunWorkerCompleted += onRunWorkerCompleted;
// Local event handlers
backgroundWorker1.ProgressChanged += backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted += backgroundWorker1_RunWorkerCompleted;
// A ProgressChanged handler has been provided so the ProgressBar will be updated explicitly based on the BackgroundWorker.
backgroundWorker1.WorkerReportsProgress = true;
progressBar1.Style = ProgressBarStyle.Continuous;
}
#endregion Constructors
#region Properties
public bool SupportsCancellation
{
set
{
backgroundWorker1.WorkerSupportsCancellation = value;
// If the worker can be cancelled, show the Cancel button and make the form big enough to see it.
cancelWorkButton.Visible = value;
ClientSize = new Size(284,
value ? 76 : 47);
}
}
#endregion Properties
#region Methods
private void BackgroundWorkerForm_Shown(object sender, System.EventArgs e)
{
// Start the background work when the form is displayed.
backgroundWorker1.RunWorkerAsync();
}
private void cancelWorkButton_Click(object sender, System.EventArgs e)
{
// Disable the button to prevent another click.
cancelWorkButton.Enabled = false;
// Cancel the background work.
backgroundWorker1.CancelAsync();
}
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
// Update the ProgressBar.
progressBar1.Value = e.ProgressPercentage;
}
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
// Close the form when the work is done.
Close();
}
private void BackgroundWorkerForm_FormClosing(object sender, FormClosingEventArgs e)
{
// Remote event handlers.
if (onDoWork != null)
{
backgroundWorker1.DoWork -= onDoWork;
}
if (onProgressChanged != null)
{
backgroundWorker1.ProgressChanged -= onProgressChanged;
}
if (onRunWorkerCompleted != null)
{
backgroundWorker1.RunWorkerCompleted -= onRunWorkerCompleted;
}
// Local event handlers
backgroundWorker1.ProgressChanged -= backgroundWorker1_ProgressChanged;
backgroundWorker1.RunWorkerCompleted -= backgroundWorker1_RunWorkerCompleted;
}
#endregion Methods
}
}