Click here to Skip to main content
14,829,025 members
Articles / Programming Languages / C# 4.0
Tip/Trick
Posted 11 Jan 2017

Stats

35.1K views
5.2K downloads
17 bookmarked

Simple Display Dialog of Waiting in WinForms

Rate me:
Please Sign up or sign in to vote.
4.85/5 (14 votes)
11 Jan 2017CPOL
A simple method to display a waiting dialog in WinForm application, and not block the main form.

Introduction

This article describes a simple method to display a waiting dialog and not block the main form.

Background

When I create a WinForm application by C#, it's very troublesome to show a waiting dialog. Sometimes I do it, but not beautiful or hard to change style.

Structure

I use a class library, WaitForm is a waiting dialog, and WaitWndFun is a class to show the form or close by create thread,

WaitWndFun Class Function

Show Function

C#
...
public void Show(Form parent)
        {
            loadthread = new Thread(new ParameterizedThreadStart(LoadingProcessEx));
            loadthread.Start(parent);
        }
...

Close Function

C#
public void Close()
        {
            if (loadingForm != null)
            {
                loadingForm.BeginInvoke(new System.Threading.ThreadStart(loadingForm.CloseLoadingForm));
                loadingForm = null;
                loadthread = null;
            }
        }

Thread Method

C#
private void LoadingProcessEx()
        {
            loadingForm = new WaitForm();
            loadingForm.ShowDialog();
        }
        private void LoadingProcessEx(object parent)
        {
            Form Cparent = parent as Form;
            loadingForm = new WaitForm(Cparent);
            loadingForm.ShowDialog();
        }

When closing dialog, there is a function to close and dispose the form in WaitForm.

C#
public void CloseLoadingForm()
        {
            this.DialogResult = DialogResult.OK;
            this.Close();
            if (label1.Image != null)
            {
                label1.Image.Dispose();
            }
        }

And when there is a parent form, we should show dialog in center of parent.

C#
public WaitForm(Form parent)
        {
            InitializeComponent();
            if (parent != null)
            {
                this.StartPosition = FormStartPosition.Manual;
                this.Location = new Point(parent.Location.X + parent.Width / 2 - this.Width / 2, 
                                parent.Location.Y + parent.Height / 2 - this.Height / 2);
            }
            else
            {
                this.StartPosition = FormStartPosition.CenterParent;
            }
        }

How to Use

It is very easy to use.

  1. Reference the WaitWnd.dll or add sources file to your project.
  2. Change the style of waiting form, you can change the picture and text of label to your liking in the form designer.

  3. Create a new WaitWndFun object. When you call show function, the waiting form will appear, and when you call close function, the waiting form will close.
    C#
    WaitWnd.WaitWndFun waitForm = new WaitWnd.WaitWndFun();
            private void button1_Click(object sender, EventArgs e)
            {
                waitForm.Show(this);
    
                //do something
                waitForm.Close();
            }

License

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

Share

About the Author

Dean Feng
Software Developer Artosyn
China China
A Software Developer.Program langauge:C#,C++,Python; Framework:Qt,VTK,MFC,Winform

Comments and Discussions

 
QuestionGood project Pin
Honokata Zai23-Mar-21 16:36
MemberHonokata Zai23-Mar-21 16:36 
Questionchanging form texts Pin
Member 1458828427-Dec-20 2:17
MemberMember 1458828427-Dec-20 2:17 
hi how can i change label text
QuestionBlocking form Pin
atin124-Jun-20 1:16
Memberatin124-Jun-20 1:16 
Questionbeginner doubt Pin
eliziarioemerson21-Mar-20 14:14
Membereliziarioemerson21-Mar-20 14:14 
Questiongood job Pin
wshcdr21-Nov-18 13:52
Memberwshcdr21-Nov-18 13:52 
QuestionWindows 7 or Xp error Pin
selcukpeksen12-Sep-18 5:10
Memberselcukpeksen12-Sep-18 5:10 
AnswerRe: Windows 7 or Xp error Pin
selcukpeksen12-Sep-18 21:19
Memberselcukpeksen12-Sep-18 21:19 
AnswerRe: Windows 7 or Xp error Pin
Saqo1995s28-Oct-19 23:11
MemberSaqo1995s28-Oct-19 23:11 
QuestionLove It! Pin
Graham Irons20-Feb-17 21:32
MemberGraham Irons20-Feb-17 21:32 
AnswerRe: Love It! Pin
Dean Feng21-Feb-17 3:31
professionalDean Feng21-Feb-17 3:31 
GeneralGood but... Pin
loyal ginger17-Jan-17 3:37
Memberloyal ginger17-Jan-17 3:37 
GeneralRe: Good but...[Thank you] Pin
Dean Feng17-Jan-17 15:42
professionalDean Feng17-Jan-17 15:42 
GeneralRe: Good but...[Thank you] Pin
loyal ginger18-Jan-17 10:11
Memberloyal ginger18-Jan-17 10:11 
GeneralRe: Good but...[Thank you] Pin
Bigby29-Apr-19 23:15
MemberBigby29-Apr-19 23:15 
QuestionNICE Pin
DumpsterJuice13-Jan-17 8:21
MemberDumpsterJuice13-Jan-17 8:21 

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.