Click here to Skip to main content
15,879,535 members
Articles / Desktop Programming / Windows Forms
Tip/Trick

Wait progress bar for long running UI operations

Rate me:
Please Sign up or sign in to vote.
2.33/5 (3 votes)
20 Sep 2011CPOL 41.9K   9   9
Showing progress bar while performing time consume processes.

Quite commonly, when working with WinForms, we perform time consume processes, for example, search on click, do some business logic, and delete entities from DB on some action.


This is the class you can add into the UI infrastructure for displaying the progress:


C#
public class WaitIndicator : IDisposable
{
    ProgressForm progressForm;
    Thread thread;
    bool disposed = false; //to avoid redundant call
    public WaitIndicator()
    {
        progressForm = new ProgressForm();
        thread = new Thread(_ => progressForm.ShowDialog());
        thread.Start();
    }

    public void Dispose()
    {
        Dispose(true);
    }

    protected virtual void Dispose(bool disposing)
    {
        if (!disposed)
        {
            thread.Abort();
            progressForm = null;
        }
        disposed = true;
    }
}

class ProgressForm : Form
{
    public ProgressForm()
    {
        ControlBox = false;
        ShowInTaskbar = false;
        StartPosition = FormStartPosition.CenterScreen;
        TopMost = true;
        FormBorderStyle = FormBorderStyle.None;
        var progreassBar = new ProgressBar() { Style = ProgressBarStyle.Marquee, 
            Size = new System.Drawing.Size(200, 20), 
            ForeColor = Color.Orange, MarqueeAnimationSpeed = 40 };
        Size = progreassBar.Size;
        Controls.Add(progreassBar);
    }
}

and usage will be like:


C#
using (new WaitIndicator())
{
    //code to process
}

License

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


Written By
India India
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions

 
GeneralRe: newly created thread is simply calling ShowDialog on instanc... Pin
kris44427-Oct-11 0:59
kris44427-Oct-11 0:59 
GeneralRe: Yes you are missing alot. UI Elements normally function by s... Pin
FZelle26-Sep-11 23:41
FZelle26-Sep-11 23:41 
GeneralYou'll generate cross-threading exceptions, or if not, your ... Pin
Bruce Lee Harrison18-Oct-11 10:41
Bruce Lee Harrison18-Oct-11 10:41 
GeneralSo what (bad) actually happens when one uses more then one U... Pin
Niemand2510-Oct-11 21:43
professionalNiemand2510-Oct-11 21:43 
GeneralReason for my vote of 1 Don't use another UI thread Pin
johannesnestler21-Sep-11 4:24
johannesnestler21-Sep-11 4:24 
GeneralRe: Since many of the winform applications uses Thread model as ... Pin
kris44427-Oct-11 0:57
kris44427-Oct-11 0:57 
GeneralWhat FZelle said is true: Yes there is a convention: Only on... Pin
johannesnestler21-Sep-11 4:23
johannesnestler21-Sep-11 4:23 
GeneralReason for my vote of 1 This exactly how you shouldn't imple... Pin
FZelle21-Sep-11 0:07
FZelle21-Sep-11 0:07 
GeneralRe: Hi When you say UI thread is it main thread you are pointing... Pin
kris44421-Sep-11 0:23
kris44421-Sep-11 0:23 

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.