Click here to Skip to main content
Click here to Skip to main content

Tagged as

Wait progress bar for long running UI operations

, 20 Sep 2011 CPOL
Rate this:
Please Sign up or sign in to vote.
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:

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());
    public void Dispose()
    protected virtual void Dispose(bool disposing)
        if (!disposed)
            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;

and usage will be like:

using (new WaitIndicator())
    //code to process


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


About the Author


India India
No Biography provided

Comments and Discussions

GeneralRe: newly created thread is simply calling ShowDialog on instanc... Pinmemberkris44427-Oct-11 1:59 
GeneralRe: Yes you are missing alot. UI Elements normally function by s... PinmemberFZelle27-Sep-11 0:41 
GeneralYou'll generate cross-threading exceptions, or if not, your ... PinmemberBruce Lee Harrison18-Oct-11 11:41 
GeneralSo what (bad) actually happens when one uses more then one U... PinmemberNiemand2510-Oct-11 22:43 
So what (bad) actually happens when one uses more then one UI thread? (especially taking into account the simplicity of the case)
GeneralReason for my vote of 1 Don't use another UI thread Pinmemberjohannesnestler21-Sep-11 5:24 
GeneralRe: Since many of the winform applications uses Thread model as ... Pinmemberkris44427-Oct-11 1:57 
GeneralWhat FZelle said is true: Yes there is a convention: Only on... Pinmemberjohannesnestler21-Sep-11 5:23 
GeneralReason for my vote of 1 This exactly how you shouldn't imple... PinmemberFZelle21-Sep-11 1:07 
GeneralRe: Hi When you say UI thread is it main thread you are pointing... Pinmemberkris44421-Sep-11 1:23 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150414.1 | Last Updated 20 Sep 2011
Article Copyright 2011 by kris444
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid