Devlico.Us
CodeBetter.Com
RSS 2.0 via Feedburner
           Do you Twitter? Follow us @devlicious

Derik Whittaker

Thoughts on Software Development, .Net, OOP, Design Patterns and all things cool



Howto: Create a Simple Hourglass/Wait Dialog

Have you ever wanted to create a ‘Busy’ or ‘Working’ windows so that your background processing that takes some time to complete does not appear to take that long?  The good news is that in .net there is a pretty simple way to accomplish this and you don’t have to do any fancy threading tricks or delegate calls.

 

This can be accomplished by using the ‘using’ keyword in .net and having a form that only exists in the scope of the using statement.

 

In this example we will create 2 files

1)       Hourglass – class for handling the open/close

2)       HourglassForm – the form to be displayed

 

We will also create a standard form for demo purposes to open the HourglassForm.

 
Before we get started, below is a snippet of the code needed to open the
HourglassForm, perform the necessary background processing and finally closes the form.

 

    using ( new Hourglass( "Text To Show User" ) )

    {

        // Do something here

    }

 

So, now that we have seen the code needed to display the HourglassForm, lets look at the main code that does all the work.  This code is in the constructor of the Hourglass class in order to be used in the ‘using’ statement.

        /// <summary>

        /// Basic constructor will open/show the hourglass Dialog

        /// </summary>

        /// <param name="message">The text to display to the user</param>

        public Hourglass( string message )

        {

            // Grab the parent form in order to open the dialog with the correct reference

            ParentForm = Form.ActiveForm;

 

            // Create and open the new hourglass dialog

            WorkingDialog = new HourglassDialog();

            WorkingDialog.StartPosition = FormStartPosition.CenterScreen;

 

            if ( message != null ) { WorkingDialog.Message = message; }

 

            // Attempt to set the curosr if we have a form.

            //  this will be reset later

            if ( ParentForm != null )

            {

                OriginalCursor = ParentForm.Cursor;

                ParentForm.Cursor = Cursors.WaitCursor;

                WorkingDialog.Show( ParentForm );

            }

            else

            {

                WorkingDialog.Show();

            }

 

            WorkingDialog.Refresh();

        }

 

Below is the code for the entire Hourglass class, this is the controlling class.  It is in this class that the form will be created, cursor will be changed and finally the form will be closed.


    public class Hourglass : IDisposable

    {

        #region "Class Variables"

 

        private Cursor _originalCursor;

        private Form _parentForm;

        private HourglassDialog _workingDialog;

 

        #endregion

 

        #region "Constructors"

 

        /// <summary>

        /// Null construct, pass in null to the secondary constuctor

        /// </summary>

        public Hourglass()

            : this( null )

        {

        }

 

        /// <summary>

        /// Basic constructor will open/show the hourglass Dialog

        /// </summary>

        /// <param name="message">The text to display to the user</param>

        public Hourglass( string message )

        {

            // Grab the parent form in order to open the dialog with the correct reference

            ParentForm = Form.ActiveForm;

 

            // Create and open the new hourglass dialog

            WorkingDialog = new HourglassDialog();

            WorkingDialog.StartPosition = FormStartPosition.CenterScreen;

 

            if ( message != null ) { WorkingDialog.Message = message; }

 

            // Attempt to set the curosr if we have a form.

            //  this will be reset later

            if ( ParentForm != null )

            {

                OriginalCursor = ParentForm.Cursor;

                ParentForm.Cursor = Cursors.WaitCursor;

                WorkingDialog.Show( ParentForm );

            }

            else

            {

                WorkingDialog.Show();

            }

 

            WorkingDialog.Refresh();

        }

 

        #endregion

 

        #region "Private Methods"

 

        /// <summary>

        /// Will close down the dialog _parentForm

        /// </summary>

        private void HideForm()

        {

            if ( WorkingDialog != null )

            {

                WorkingDialog.Close();

                WorkingDialog = null;

            }

        }

 

        /// <summary>

        /// Swap the cursor out for the original/passed in cursor.

        /// </summary>

        private void ResetCursor()

        {

            if ( ParentForm != null )

            {

                ParentForm.Cursor = OriginalCursor;

            }

 

        }

 

        #endregion

 

        #region "Private Properties"

 

        private Form ParentForm

        {

            get { return _parentForm; }

            set { _parentForm = value; }

        }

 

        private Cursor OriginalCursor

        {

            get { return _originalCursor; }

            set { _originalCursor = value; }

        }

 

        private HourglassDialog WorkingDialog

        {

            get { return _workingDialog; }

            set { _workingDialog = value; }

        }

 

        #endregion

 

        #region IDisposable Members

 

        public void Dispose()

        {

            HideForm();

            ResetCursor();

        }

 

        #endregion

    }


See how simple it can be to create a Hourglass/Wait dialog in .net.

Let me know if you have a different/better way to accomplish this same task.
 

 



Comments

ashu fouzdar said:

HI

The code looks nice but I am unable to find any code for Hourglassdialog  class.  

# September 27, 2007 3:29 AM

Karina Simard said:

I made a simple HourglassDialog class, only thing it needs is a Message Property...  and it works great.

# May 5, 2008 7:17 PM

Leave a Comment

(required)  
(optional)
(required)  

Enter the numbers above:
Add

About Derik Whittaker

Derik is a .Net Developer/Architect specializing in WinForms working out the northern suburbs of Chicago. He is also believer and advocate for Agile development including SCRUM, TDD, CI, etc.

When Derik is not writing code he can be found spending time with his wife and young son, climbing on his bouldering wall, watching sports (mostly baseball), and generally vegging out. Check out Devlicio.us!

Our Sponsors

Red-Gate!