Click here to Skip to main content
15,896,348 members
Articles / Desktop Programming / Windows Forms

AViD

Rate me:
Please Sign up or sign in to vote.
4.81/5 (18 votes)
3 Mar 2009CPOL10 min read 53.8K   1.2K   24  
An application for visualizing common dendrimer models
#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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Architect
United States United States
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.

Comments and Discussions