Click here to Skip to main content
15,881,568 members
Articles / Programming Languages / C#
Tip/Trick

Please Wait Window Example

Rate me:
Please Sign up or sign in to vote.
4.88/5 (6 votes)
28 Apr 2014CPOL2 min read 26.2K   938   16   9
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:

C#
/// <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.

C#
/// <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)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
QuestionShowDialog doesnt work, Am Im missing smth? Pin
Talles Santana18-Sep-16 5:17
Talles Santana18-Sep-16 5:17 
Hello there.
I tryed to use the PleaseWaitForm in my application.
I Copied the files PleaseWaitForm(cs, designer, resx) and Shared.cs and SharedResourses(resx, designer.cs) to the root folder and checked the .csproj files to be sure the assemblies references are the equal and would compile fine.

I followed the sample, but it didn't work. Using the debug I could check that ShowDialog() just goes to the next line. Without showing the please wait window.
C#
private Object Show(EventHandler<PleaseWaitWindowEventArgs> pWorkerMethod, String pPleaseWaitTitle, String pDescription, Boolean pWaitToClose, Image pNewImage, List<Object> args)
{

this.m_GUI.ShowDialog(); //this line is doing nothing. just goes to the next line.


Did someone have the same problem? Am I missing something?

Thanks in Advance,
Talles
GeneralMy vote of 5 Pin
Volynsky Alex29-Apr-14 23:01
professionalVolynsky Alex29-Apr-14 23:01 
GeneralRe: My vote of 5 Pin
MSBassSinger30-Apr-14 4:46
professionalMSBassSinger30-Apr-14 4:46 
GeneralRe: My vote of 5 Pin
Volynsky Alex30-Apr-14 11:18
professionalVolynsky Alex30-Apr-14 11:18 
QuestionSomething is missing here? Pin
novice10128-Apr-14 21:04
novice10128-Apr-14 21:04 
AnswerRe: Something is missing here? Pin
MSBassSinger28-Apr-14 23:19
professionalMSBassSinger28-Apr-14 23:19 
GeneralRe: Something is missing here? Pin
Johann Krenn29-Apr-14 0:00
Johann Krenn29-Apr-14 0:00 
GeneralRe: Something is missing here? Pin
MSBassSinger29-Apr-14 0:04
professionalMSBassSinger29-Apr-14 0:04 
GeneralRe: Something is missing here? Pin
MSBassSinger29-Apr-14 8:25
professionalMSBassSinger29-Apr-14 8:25 

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

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