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

Tagged as

Go to top

Wait progress bar for long running UI operations

, 20 Sep 2011
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());
        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:

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)

Share

About the Author

kris444

India India
No Biography provided

Comments and Discussions

 
GeneralSo what (bad) actually happens when one uses more then one U... PinmemberNiemand2510-Oct-11 21:43 

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 | Mobile
Web03 | 2.8.140916.1 | Last Updated 20 Sep 2011
Article Copyright 2011 by kris444
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid