//Except where stated all code and programs in this project are the copyright of Jim Blackler, 2008.
//jimblackler@gmail.com
//
//This is free software. Libraries and programs are distributed under the terms of the GNU Lesser
//General Public License. Please see the files COPYING and COPYING.LESSER.
using System;
using System.ComponentModel;
using System.Windows.Forms;
namespace MiscControls
{
///
/// Provides background worker functionality combined with a progress bar and status message.
///
public partial class VisualBackgroundWorker : UserControl
{
///
/// Gets a value indicating whether the application has requested cancellation
/// of the background operation.
///
[Browsable(false)]
public bool CancellationPending
{
get
{
return backgroundWorker1.CancellationPending;
}
}
///
/// Gets a value indicating whether the System.ComponentModel.BackgroundWorker
/// is running an asynchronous operation.
///
[Browsable(false)]
public bool IsBusy
{
get
{
return backgroundWorker1.IsBusy;
}
}
///
/// Gets or sets a value indicating whether the System.ComponentModel.BackgroundWorker
/// supports asynchronous cancellation.
///
[Category("Worker")]
[DefaultValue(false)]
public bool WorkerSupportsCancellation
{
get
{
return backgroundWorker1.WorkerSupportsCancellation;
}
set
{
backgroundWorker1.WorkerSupportsCancellation = value;
}
}
///
/// Perform the background worker's async operation.
///
[Category("Worker")]
public event DoWorkEventHandler DoWork
{
add
{
doWork += value;
}
remove
{
doWork -= value;
}
}
private DoWorkEventHandler doWork;
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
if (doWork != null)
{
doWork.Invoke(this, e);
}
}
///
/// Fires when the progress on the background worker is changed.
///
[Category("Worker")]
public event ProgressChangedEventHandler ProgressChanged
{
add
{
progressChanged += value;
}
remove
{
progressChanged -= value;
}
}
private ProgressChangedEventHandler progressChanged;
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
label1.Text = (string)e.UserState;
if (progressChanged != null)
{
progressChanged.Invoke(this, e);
}
}
///
/// The text currently displayed by the visual background worker (read only, set with ReportProgress).
///
public override string Text
{
get { return label1.Text; }
}
///
/// Fires when the background event is reported complete.
///
[Category("Worker")]
public event RunWorkerCompletedEventHandler RunWorkerCompleted
{
add
{
runWorkerCompleted += value;
}
remove
{
runWorkerCompleted -= value;
}
}
private RunWorkerCompletedEventHandler runWorkerCompleted;
private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
{
if (runWorkerCompleted != null)
{
runWorkerCompleted.Invoke(this, e);
}
}
///
/// Requests cancellation of a pending background operation.
///
public void CancelAsync()
{
backgroundWorker1.CancelAsync();
}
///
/// Raises the System.ComponentModel.BackgroundWorker.ProgressChanged event.
///
/// The percentage, from 0 to 100, of the background operation that is complete.
public void ReportProgress(int percentProgress)
{
backgroundWorker1.ReportProgress(percentProgress);
}
///
/// Raises the System.ComponentModel.BackgroundWorker.ProgressChanged event.
///
/// The percentage, from 0 to 100, of the background operation that is complete.
/// The state object passed to System.ComponentModel.BackgroundWorker.RunWorkerAsync(System.Object).
public void ReportProgress(int percentProgress, object userState)
{
backgroundWorker1.ReportProgress(percentProgress, userState);
}
///
/// Starts execution of a background operation.
///
public void RunWorkerAsync()
{
backgroundWorker1.RunWorkerAsync();
}
///
/// Starts execution of a background operation.
///
/// A parameter for use by the background operation to be executed in the System.ComponentModel.BackgroundWorker.DoWork
/// event handler
public void RunWorkerAsync(object argument)
{
backgroundWorker1.RunWorkerAsync(argument);
}
///
/// The object as BackgroundWorker
///
public BackgroundWorker BackgroundWorker
{
get
{
return backgroundWorker1;
}
}
///
/// Constructor for the control.
///
public VisualBackgroundWorker()
{
InitializeComponent();
label1.Text = "";
}
///
/// Provide access to the progress bar control that forms part of the visual background worker.
///
public ProgressBar GetProgressBar()
{
return progressBar1;
}
}
}