Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# - ProgressBar with Marquee style

I'd like to add a ProgressBar on a form with Marquee style, to show the user something is in progress. During the time consuming operation the form doesn't get updated, so the ProgressBar also "freezes".

I've checked several posts about BackgroundWorker, but in my case the operation doesn't report progress, that's why I need a Marquee bar.

Any help or code snippet is appreciated.

Note: I need to use .NET 4.0 (for XP support) so I can't use Task.Run :(

button1_Click(object sender, EventArgs e)
{
    progressBar1.Style = ProgressBarStyle.Marquee;
    progressBar1.MarqueeAnimationSpeed = 50;

    // INSERT TIME CONSUMING OPERATIONS HERE
    // THAT DON'T REPORT PROGRESS
    Thread.Sleep(10000);

    progressBar1.MarqueeAnimationSpeed = 0;
    progressBar1.Style = ProgressBarStyle.Blocks;
    progressBar1.Value = progressBar1.Minimum;

}
like image 831
dobragab Avatar asked Sep 02 '25 18:09

dobragab


1 Answers

I've checked several posts about BackgroundWorker, but in my case the operation doesn't report progress, that's why I need a Marquee bar.

You can use the BackgroundWorker, just don't use the "progress" portion of it. These two things are not mutually exclusive...

Example:

    private void button1_Click(object sender, EventArgs e)
    {
        button1.Enabled = false;
        progressBar1.Style = ProgressBarStyle.Marquee;
        progressBar1.MarqueeAnimationSpeed = 50;

        BackgroundWorker bw = new BackgroundWorker();
        bw.DoWork += bw_DoWork;
        bw.RunWorkerCompleted += bw_RunWorkerCompleted;
        bw.RunWorkerAsync();
    }

    void bw_DoWork(object sender, DoWorkEventArgs e)
    {
        // INSERT TIME CONSUMING OPERATIONS HERE
        // THAT DON'T REPORT PROGRESS
        Thread.Sleep(10000);
    }

    void bw_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
    {
        progressBar1.MarqueeAnimationSpeed = 0;
        progressBar1.Style = ProgressBarStyle.Blocks;
        progressBar1.Value = progressBar1.Minimum;

        button1.Enabled = true;
        MessageBox.Show("Done!");
    }
like image 72
Idle_Mind Avatar answered Sep 04 '25 06:09

Idle_Mind