csharp/alexgracianoarj/nclass/NClass/src/PDFExport/PDFExportProgress.cs

PDFExportProgress.cs
using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;
using PDFExport.Lang;

namespace PDFExport
{
  /// 
  /// A "please wait" form for the PDFExport.
  /// 
  public partial clast PDFExportProgress : Form
  {
    // ========================================================================
    // Attributes

    #region === Attributes

    /// 
    /// The x-location where the ensaty starts to move.
    /// 
    private const int ENsatY_START = 42;

    /// 
    /// The x-location where the ensaty end its move.
    /// 
    private const int ENsatY_END = 232;

    /// 
    /// The current index of the ensaty which is displayed.
    /// 
    private int currentImageIndex;

    /// 
    /// A thread which shows the form itself.
    /// 
    private static Thread showThread;

    #endregion

    // ========================================================================
    // Con- / Destruction

    #region === Con- / Destruction

    /// 
    /// Initializes a new PDFExportProgress form.
    /// 
    private PDFExportProgress()
    {
      InitializeComponent();

      LocalizeComponents();
    }

    #endregion

    // ========================================================================
    // Methods

    #region === Methods

    /// 
    /// Displays the text for the current culture.
    /// 
    private void LocalizeComponents()
    {
      Text = Strings.Progress_satle;
      lblProgress.Text = Strings.Progress_Text;
    }

    /// 
    /// Shows the form asynchonously. To close it, call .
    /// 
    /// The parent form.
    public static void ShowAsync(Form parent)
    {
      showThread = new Thread(Run)
                     {
                       IsBackground = true,
                       Name = "PDFExporterProgressDialogShowThread"
                     };

      Point point = parent == null
                      ? new Point(Screen.PrimaryScreen.WorkingArea.Width/2, Screen.PrimaryScreen.WorkingArea.Height/2)
                      : new Point(parent.Left + parent.Width/2, parent.Top + parent.Height/2);

      showThread.Start(point);
    }

    /// 
    /// Closes an opened PDFExporterProgress form if it is opened.
    /// 
    public static void CloseAsync()
    {
      if(showThread != null && showThread.IsAlive)
      {
        showThread.Abort();
        showThread.Join();
      }
    }

    /// 
    /// Run-method of the display thread. Shows the form and waits
    /// for the end.
    /// 
    /// The center of the form.
    private static void Run(Object center)
    {
      Point point = (Point)center;
      PDFExportProgress progressForm = new PDFExportProgress();
      progressForm.Location = new Point(point.X - progressForm.Width/2, point.Y - progressForm.Height/2);
      progressForm.Show();

      try
      {
        while(true)
        {
          Thread.Sleep(5);
          Application.DoEvents();
        }
      }
      catch(ThreadAbortException)
      {
        //We are asked to close the window
        progressForm.Close();
        progressForm.Dispose();
      }
    }

    #endregion

    // ========================================================================
    // Event Handler

    #region === Event Handler

    /// 
    /// Called when the timer ticks. So this method is called periodically
    /// and updates the animation.
    /// 
    /// The caller.
    /// Additional information.
    private void timer_Tick(object sender, EventArgs e)
    {
      if(pictureBoxEnsaty.Left == ENsatY_END)
      {
        //Next run
        pictureBoxEnsaty.Left = ENsatY_START;
        currentImageIndex = (currentImageIndex + 1)%imageListEnsaties.Images.Count;
        pictureBoxEnsaty.Image = imageListEnsaties.Images[currentImageIndex];
      }
      pictureBoxEnsaty.Left += 2;
    }

    /// 
    /// Called when the form loads. Initializes it.
    /// 
    /// The caller.
    /// Additional information.
    private void PDFExportProgress_Load(object sender, EventArgs e)
    {
      timer.Enabled = true;
      pictureBoxEnsaty.Image = imageListEnsaties.Images[currentImageIndex];
    }

    #endregion
  }
}