How to create a Metro Wait Form in C#
By FoxLearn 11/20/2024 12:58:21 PM 5.18K
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
Add a new Form
to your project and name it frmMain
, then you can design your metro form as shown below.
Add a new Form
to your project and name it frmWaitForm
.
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
- How to use Modern UI Metro Framework in C#
- How to Create CPU and Memory Monitor using Metro Modern UI in C#
- How to Create a Metro Modern Flat UI Dashboard in C#
- How to Download and Install Metro Framework
- How To Use Metro Framework Metro Style Manager in C#
- How to Create a Modern Windows 8 UI with the Metro Framework in C#
- How to create a Metro Message Box in C#
- How to use Metro Progress Bar in C#