|
#region Using Statements
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
#endregion Using Statements
namespace AViD
{
/// <summary>
/// UI Element to initiate an asynchronous operation, which will provide information through
/// a progress bar.
/// </summary>
public partial class ProgressBar : Form
{
#region Constructor
/// <summary>
/// Creates a new instance of the ProgressBar.
/// </summary>
public ProgressBar(IWorker worker)
{
InitializeComponent();
// Wire up the events.
this.m_worker = worker;
this.m_worker.Progress += new EventHandler<ProgressEventArgs>(ProgressBar_Progress);
this.m_worker.Complete += new EventHandler(ProgressBar_Complete);
// Begin the work.
this.m_worker.Work();
}
#endregion Constructor
#region Events/Delegates
/// <summary>
/// Handles the event when the user clicks the Abort button.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> of the event.</param>
private void btnAbort_Click(object sender, EventArgs e)
{
// Unwire the events.
this.m_worker.Progress -= new EventHandler<ProgressEventArgs>(ProgressBar_Progress);
this.m_worker.Complete -= new EventHandler(ProgressBar_Complete);
// Inform the worker to abort.
this.m_worker.Abort();
this.DialogResult = DialogResult.Cancel;
this.Close();
}
/// <summary>
/// Handles the event when the <see cref="IWorker"/> is done.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="EventArgs"/> of the event.</param>
private void ProgressBar_Complete(object sender, EventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new EventHandler(ProgressBar_Complete), sender, e);
}
else if (!this.IsDisposed)
{
this.btnAbort.Enabled = false;
// Unwire the events.
this.m_worker.Progress -= new EventHandler<ProgressEventArgs>(ProgressBar_Progress);
this.m_worker.Complete -= new EventHandler(ProgressBar_Complete);
this.DialogResult = DialogResult.OK;
this.Close();
}
}
/// <summary>
/// Handles the event when the <see cref="IWorker"/> signals progress.
/// </summary>
/// <param name="sender">The sender of the event.</param>
/// <param name="e">The <see cref="ProgressEventArgs"/> of the event.</param>
private void ProgressBar_Progress(object sender, ProgressEventArgs e)
{
if (this.InvokeRequired)
{
this.Invoke(new EventHandler<ProgressEventArgs>(ProgressBar_Progress), sender, e);
}
else if (!this.IsDisposed)
{
this.pbProgress.Value = ((int)(e.PercentComplete * 100.0d));
}
}
#endregion Events/Delegates
#region Private Members
private readonly IWorker m_worker;
#endregion Private Members
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
Since I've begun my profession as a software developer, I've learned one important fact - change is inevitable. Requirements change, code changes, and life changes.
So..If you're not moving forward, you're moving backwards.