How to create a Metro Wait Form in C#

By FoxLearn 11/20/2024 12:58:21 PM   5.18K
To create a Metro-style WaitForm dialog in C# using the Metro Framework, you can follow these steps.

Open Visual Studio, then click New Project, then select Visual C# on the left, then Windows and then select Windows Forms Application. Name your project "MetroWaitForm" and then click OK

If you haven't already installed the Metro Framework, add it to your project via Manage NuGet Packages.

Right click on your project select Manage NuGet Packages -> Search metro framework -> Install

install metro framework

Add a new Form to your project and name it frmMain, then you can design your metro form as shown below.

metro wait form in c#

Add a new Form to your project and name it frmWaitForm.

metro waitform in c#

Add code to handle frmWaitForm

This form handles the display of a wait dialog while performing a long-running task in the background.

using System;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WaitFormDemo
{
    public partial class frmWaitForm : Form
    {
        // Action to be executed in the background
        public Action Worker { get; set; }
        public frmWaitForm(Action worker)
        {
            InitializeComponent();
            if (worker == null)
                throw new ArgumentNullException();
            Worker = worker;
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);
            // Start a new thread for the worker action
            Task.Factory.StartNew(Worker).ContinueWith(t => { this.Close(); }, TaskScheduler.FromCurrentSynchronizationContext());
        }
    }
}

Constructor: We receives a delegate (Action) to define the task that will execute while the wait form is displayed.

OnLoad Method: Handles execution of the Worker action in a background thread (Task.Factory.StartNew) and ensures the form is closed on the main thread when the task is complete.

Add code to handle frmMain

This form contains the logic to trigger the frmWaitForm while performing a simulated long-running task.

using System;
using System.Threading;

namespace MetroWaitForm
{
    public partial class frmMain : MetroFramework.Forms.MetroForm
    {
        public frmMain()
        {
            InitializeComponent();
        }

        // Simulate a save operation (long-running task)
        void Save()
        {
            //Only for demo
            for (int i = 0; i <= 100; i++)
            {
                Thread.Sleep(20); // Simulate work
                // Perform save operation (if necessary, include actual saving logic here)
            }
        }

        // Button click event to open the wait form
        private void btnSave_Click(object sender, EventArgs e)
        {
            //Open waitform dialog
            using (var waitForm = new frmWaitForm(Save))
            {
                waitForm.ShowDialog(this);
            }
        }
    }
}

Save Method: A dummy long-running operation that simulates a task using Thread.Sleep.

btnSave_Click: Creates and displays an instance of frmWaitForm while passing the Save method as the worker task.

When the user clicks the Save button, the btnSave_Click method is invoked.

Next, The frmWaitForm is displayed, and the Save method runs in a background thread.

Once the Save method completes, the frmWaitForm closes automatically.

VIDEO TUTORIAL