Click here to Skip to main content
Click here to Skip to main content

Please Wait Window Example

, 28 Apr 2014
Rate this:
Please Sign up or sign in to vote.
Creating and using a PleaseWait window with feedback and cancel.

Introduction

If you have code that needs to execute, and you want the user to wait on it, having a "Please Wait" window is helpful. This example (see full code in attached ZIP file) shows one way to do that. The window the user sees can have text that is updated during the process (that runs in another thread), an image (you can use the default or provide one of your own programmatically), and you can cancel the process while it is running.

Background

Over the years, I have seen a multitude of all sorts of convoluted ways to do this. Without multithreading, such as in VB6, you could use events and that worked OK, but it was not as responsive as multithreading.

Using the Code

PleaseWaitForm.cs contains the code, which has multiple classes. In short, there is a sealed class with static methods and those methods create and use a Windows form as the Please Wait form, and execute a specified method on a non-UI thread.

In MainForm.cs, the StartButton executes this code to bring up the Please Wait window and start the process:

/// <summary>  
/// Fires when the Start button is clicked. 
/// </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
private void StartButton_Click(object sender, EventArgs e) 
{ 
    StartButton.Enabled = false; 
 
    Boolean KeepWindowOpen = KeepWindowOpenCheckBox.Checked;

    PleaseWaitWindow.Show(this.Work2Do, KeepWindowOpen);

    StartButton.Enabled = true;


}  // END private void StartButton_Click(object sender, EventArgs e) 

Note that PleaseWaitWIndow.Show is a static method. The Boolean KeepWindowOpen tells the Please Wait window whether to stay open after completing the process so the user can read the feedback.

This code is the method actually doing the work, and how it interacts with the Please Wait window.

/// <summary> 
/// This is a method illustrating something being done 
/// while the Please Wait window is showing.  
///  </summary> 
/// <param name="sender"></param> 
/// <param name="e"></param> 
private void Work2Do(Object sender, PleaseWaitWindowEventArgs e) 
{
    // Type-specific variable to use the sender variable. 
    PleaseWaitForm PWWindow = null;

    try  
    {
        if (sender != null) 
        {

            if (sender is PleaseWaitForm) 
            { 
                // We want to be sure the sender is what we expect. 
                PWWindow = (PleaseWaitForm)sender; 
            }
        }

        // PWWindow is not null ONLY if it is an instantiated PleaseWaitWindow object. 
        if (PWWindow != null) 
        {
            PWWindow.Invoke(new PleaseWaitWindow.MethodInvokerDelegate<String>
            (PWWindow.SetMessage), "Please Wait while Counting Down ..."); 
            PWWindow.Invoke(new PleaseWaitWindow.MethodInvokerDelegate<String>
            (PWWindow.SetDescription), "Counting down from 10" + Environment.NewLine + Environment.NewLine);

            for (int i = 10; i > 0; i--) 
            {
                String PWMsg = String.Format("{0}...{1}", 
                    i.ToString(), Environment.NewLine);

                // Checking "InvokeRequired" prevents an error if Cancel 
                // is selected and the Invoke method is called before cancel takes effect. 
                if (PWWindow.InvokeRequired) 
                { 
                    PWWindow.Invoke(new PleaseWaitWindow.MethodInvokerDelegate<String>(PWWindow.AppendDescription), PWMsg); 
                }

                // Using this for the countdown illustration. 
                System.Threading.Thread.Sleep(1000);

            }  // END for (int i = 0; i < 10; i++)

        }  // END if (PWWindow != null)

    }  // END try

    catch (Exception exUnhandled) 
    { 
        // Debug logging for this example project. 
        // Replace it with your logging, if you want. 
        Shared.WriteToDebugFile(exUnhandled);

    }  // END catch (Exception exUnhandled)

}  // END private void Work2Do(Object sender, PleaseWaitWindowEventArgs e)

Points of Interest

We developers have a tendency to confuse how complicated our solutions are with how clever they are. The simpler the solution, the better. Cleverness is really found in simplicity. I would not be surprised if someone takes my code and makes it even simpler without losing any functionality.

History

  • 04/28/2014 JDJ Genesis
  • 04/29/2014 JDJ Updated code in ZIP file to rename Please Wait Form's Cancel button, make sure that button is disabled when the process is done, and add code for the Please Wait form's progress bar that I forgot to do.

License

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

About the Author

MSBassSinger
Software Developer (Senior)
United States United States
I have been in technology in one form or another since 1973 when I became a part of the U. S. Navy's Nuclear Power Program.
 
I started programming at Florida Atlantic University (FORTRAN IV) in 1979, and worked on computerized control and automation systems for heating and air.
 
I migrated to COBOL, some proprietary machine code, Clipper, QuickBasic, Visual Basic, and finally landing on C# with the occasional VB.NET. Twice in my career I delved into Java. Once back in the late 1990s, only to find it was just not ready for primetime on the Windows platform, compared to VB5 and 6. In 2010, I took two semesters of Java for my BS degree, made A's, but just didn't find the love for it.
 
My experience is in database design, software architecture and engineering, project management, and some experience in sales and marketing.
 
I finished my MBA (Masters in Business Administration)in March of 2013 with a specialization in Technology Management. Experience has taught me that one needs to understand the business side to really make the technology side work better.
Follow on   LinkedIn

Comments and Discussions

 
GeneralMy vote of 5 PinpremiumVolynsky Alex29-Apr-14 23:01 
GeneralRe: My vote of 5 PinprofessionalMSBassSinger30-Apr-14 4:46 
GeneralRe: My vote of 5 PinpremiumVolynsky Alex30-Apr-14 11:18 
QuestionSomething is missing here? PinmemberBahman Kasmai28-Apr-14 21:04 
AnswerRe: Something is missing here? PinprofessionalMSBassSinger28-Apr-14 23:19 
GeneralRe: Something is missing here? PinmemberJohann Krenn29-Apr-14 0:00 
GeneralRe: Something is missing here? PinprofessionalMSBassSinger29-Apr-14 0:04 
GeneralRe: Something is missing here? PinprofessionalMSBassSinger29-Apr-14 8:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Mobile
Web02 | 2.8.140721.1 | Last Updated 29 Apr 2014
Article Copyright 2014 by MSBassSinger
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid