Derik Whittaker

Syndication

News


Images in this post missing? We recently lost them in a site migration. We're working to restore these as you read this. Should you need an image in an emergency, please contact us at imagehelp@codebetter.com
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.
 

 


Posted 10-10-2006 6:29 AM by Derik Whittaker
Filed under: , ,

[Advertisement]

Comments

Gildas wrote re: Howto: Create a Simple Hourglass/Wait Dialog
on 11-15-2006 10:26 AM

Hi,

your code is interesting, but useless without the class HourglassDialog !

ashu fouzdar wrote re: Howto: Create a Simple Hourglass/Wait Dialog
on 09-27-2007 3:29 AM

HI

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

Karina Simard wrote re: Howto: Create a Simple Hourglass/Wait Dialog
on 05-05-2008 7:17 PM

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

Amos Van Horn wrote re: Howto: Create a Simple Hourglass/Wait Dialog
on 11-26-2008 7:14 PM

Thanks so much - this is exactly what I needed and a HUGE help!

Mangesh wrote re: Howto: Create a Simple Hourglass/Wait Dialog
on 12-22-2008 12:55 AM

Hi,

I want to use GIF image over HourglassDialog.

But animation of GIF is not working. Please help

Tim wrote re: Howto: Create a Simple Hourglass/Wait Dialog
on 05-12-2009 8:42 AM

I made a simple hourglassDialog class and just put a Message property in it but it did not work for me. Please post your HourglassDialog class code as well. Thank you.

About The CodeBetter.Com Blog Network
CodeBetter.Com FAQ

Our Mission

Advertisers should contact Brendan

Subscribe
Google Reader or Homepage

del.icio.us CodeBetter.com Latest Items
Add to My Yahoo!
Subscribe with Bloglines
Subscribe in NewsGator Online
Subscribe with myFeedster
Add to My AOL
Furl CodeBetter.com Latest Items
Subscribe in Rojo

Member Projects
DimeCasts.Net - Derik Whittaker

Friends of Devlicio.us
Red-Gate Tools For SQL and .NET

NDepend

SlickEdit
 
SmartInspect .NET Logging
NGEDIT: ViEmu and Codekana
LiteAccounting.Com
DevExpress
Fixx
NHibernate Profiler
Unfuddle
Balsamiq Mockups
Scrumy
JetBrains - ReSharper
Umbraco
NServiceBus
RavenDb
Web Sequence Diagrams
Ducksboard<-- NEW Friend!

 



Site Copyright © 2007 CodeBetter.Com
Content Copyright Individual Bloggers

 

Community Server (Commercial Edition)