How to Create a Wait Form Dialog in C#

By FoxLearn 7/16/2024 8:52:11 AM   30.49K
Creating a wait dialog in a C# Windows Forms application can enhance user experience by providing feedback during long-running operations.

Open your Visual Studio, then create a new Windows Forms Application project.

How to Wait Form Dialog in C#

In C#, you can easily create a waiting dialog form to show progress or indicate that your application is busy performing some task.

Here's a basic example of how to create and use a waiting dialog form

Creating a new form, then enter your form name is frmWaitForm.

Next, Drag and drop the Label, Progress Bar controls from your Visual Studio toolbox to your winform, then you can layout your ui design as shown below.

wait form dialog in c#

This form we'll use to display the please wait form c# progress bar.

Next, create a new form with name is Form1, this form is the main form. To play the demo, you can drag the ListView and Button controls from the visual studio toolbox to your main form, then layout your main form as shown below.

wait form dialog in c#

Create the Worker property to handle loader in windows forms c# to the frmWaiForm.

public Action Worker { get; set; }

The Action is a delegate type, which is the same as Func delegate except that the Action delegate does not return a value. In other words, an Action delegate can be used with a method that has a void return type.

Adding worker parameter to the frmWaitForm constructor allows you to initalize the Worker property as the following c# code.

public frmWaitForm(Action worker)
{
    InitializeComponent();
    if (worker == null)
        throw new ArgumentNullException();
    Worker = worker;
}

And don't forget to override the OnLoad method allows you to start the new thread when closing the form.

protected override void OnLoad(EventArgs e)
{
    base.OnLoad(e);
    //Start new thread to run wait form dialog
    Task.Factory.StartNew(Worker).ContinueWith(t => { this.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());
}

In your main form or wherever you want to trigger the waiting dialog, instantiate it, show it as a non-modal dialog, perform the task that requires waiting, and then close the waiting dialog once the task is complete.

Opening your Form1, then create the SaveData method. It's a method of simulating data storage, you can use the Thread.Sleep method to delay your loop.

void SaveData()
{
    //Add code to process your data
    for (int i = 0; i <= 500; i++)
        Thread.Sleep(10); //Simulator
}

Add the button click event handler to the Save button allows you to open the loader in windows forms c#.

private void button1_Click(object sender, EventArgs e)
{
    //Open wait form dialog
    using (frmWaitForm frm = new frmWaitForm(SaveData))
    {
        // Show the waiting dialog
        frm.ShowDialog(this);
    }
}

In this example, button1_Click is an event handler for a button click event on your main form. When the button is clicked, it instantiates the waiting dialog form, shows it non-modally, performs the long-running task (SaveData()), and then closes the waiting dialog once the task is complete.

This is a very basic c# example project that shows you how to create c# progress dialog then using to create c# loading screen or c# splash screen.

VIDEO TUTORIAL