Windows Forms ProgressBar: Easiest way to start/stop marquee?

C#WinformsProgress Bar

C# Problem Overview


I am using C# and Windows Forms. I have a normal progress bar working fine in the program, but now I have another operation where the duration cannot be easily calculated. I would like to display a progress bar but don't know the best way to start/stop the scrolling marquee. I was hoping for something as simple as setting the marquee speed and then having a start() and stop() but it doesn't appear to be that simple. Do I have to run an empty loop in the background? How do I best do this? Thanks

C# Solutions


Solution 1 - C#

Use a progress bar with the style set to Marquee. This represents an indeterminate progress bar.

myProgressBar.Style = ProgressBarStyle.Marquee;

You can also use the MarqueeAnimationSpeed property to set how long it will take the little block of color to animate across your progress bar.

Solution 2 - C#

To start/stop the animation, you should do this:

To start:

progressBar1.Style = ProgressBarStyle.Marquee;
progressBar1.MarqueeAnimationSpeed = 30;

To stop:

progressBar1.Style = ProgressBarStyle.Continuous;
progressBar1.MarqueeAnimationSpeed = 0;

Solution 3 - C#

It's not how they work. You "start" a marquee style progress bar by making it visible, you stop it by hiding it. You could change the Style property.

Solution 4 - C#

This code is a part of a login form where the users wait for the authentication server to respond.

using System;
using System.ComponentModel;
using System.Threading;
using System.Windows.Forms;

namespace LoginWithProgressBar
{
    public partial class TheForm : Form
    {
        // BackgroundWorker object deals with the long running task
        private readonly BackgroundWorker _bw = new BackgroundWorker();

        public TheForm()
        {
            InitializeComponent();

            // set MarqueeAnimationSpeed
            progressBar.MarqueeAnimationSpeed = 30;

            // set Visible false before you start long running task
            progressBar.Visible = false;

            _bw.DoWork += Login;
            _bw.RunWorkerCompleted += BwRunWorkerCompleted;
        }

        private void BwRunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            // hide the progress bar when the long running process finishes
            progressBar.Hide();
        }

        private static void Login(object sender, DoWorkEventArgs doWorkEventArgs)
        {
            // emulate long (3 seconds) running task
            Thread.Sleep(3000);
        }

        private void ButtonLoginClick(object sender, EventArgs e)
        {
            // show the progress bar when the associated event fires (here, a button click)
            progressBar.Show();

            // start the long running task async
            _bw.RunWorkerAsync();
        }
    }
}    

Solution 5 - C#

There's a nice article with code on this topic on MSDN. I'm assuming that setting the Style property to ProgressBarStyle.Marquee is not appropriate (or is that what you are trying to control?? -- I don't think it is possible to stop/start this animation although you can control the speed as @Paul indicates).

Solution 6 - C#

Many good answers here already, although you also need to keep in mind that if you are doing long-running processing on the UI thread (generally a bad idea), then you won't see the marquee moving either.

Solution 7 - C#

you can use a Timer (System.Windows.Forms.Timer).

Hook it's Tick event, advance then progress bar until it reaches the max value. when it does (hit the max) and you didn't finish the job, reset the progress bar value back to minimum.

...just like Windows Explorer :-)

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMark StahlerView Question on Stackoverflow
Solution 1 - C#Paul FisherView Answer on Stackoverflow
Solution 2 - C#_lonerangerView Answer on Stackoverflow
Solution 3 - C#Hans PassantView Answer on Stackoverflow
Solution 4 - C#Arda BasogluView Answer on Stackoverflow
Solution 5 - C#tvanfossonView Answer on Stackoverflow
Solution 6 - C#Nameless OneView Answer on Stackoverflow
Solution 7 - C#AsherView Answer on Stackoverflow