-2

The Progress bar (Rollbar) does not complete before reaching the maximum, even though I have made an if statement that will only return it once complete.

[EDIT: I tried David Heffernan's answer and now it almost reaches end but not quite.]

What it looks like

I know the if statement looks weird but that's how I interpreted the answer

        private void Bartimer_Tick(object sender, EventArgs e)
        {
            if (Rollbar.Value == 1000)
            {
                Rollbar.Value = 1000;
                Rollbar.Increment(-1);
                Rollbar.Value = 0;
                Bartimer.Stop();
            }
            else
            {
                Rollbar.Increment(1);
            }
        }

        private void Rollbtn_Click(object sender, EventArgs e)
        {
            Rollbar.Minimum = 0;
            Rollbar.Maximum = 1000;
            Bartimer.Start();
        }

I have a feeling that it has something to do with the animation speed or timer interval.

I have also approached this in different ways already by trying to stop the timer after the bar reaches 0 but nothing changes.

Any suggestions would be appreciated.

Rafi Nonato
  • 73
  • 12
  • 2
    What do you mean "does not complete" ? Are you saying the value never reaches 100? Or it reaches 100 but the progress bar isn't full? Or the progress bar disappears too soon? I don't understand. There is a lot of code not included in your example. – John Wu Mar 25 '20 at 18:11
  • FYI, you don't need to set the `Step` property more than once... – Rufus L Mar 25 '20 at 18:23
  • *"The Progress bar (Rollbar) does not complete before reaching the maximum"* Isn't that by design? The maxiumum defines the completion. – Rufus L Mar 25 '20 at 18:26
  • @JohnWu What i mean is it reaches 100 but the bar isnt full, as you said. – Rafi Nonato Mar 26 '20 at 09:08
  • I need it to fill up and reset once its done – Rafi Nonato Mar 26 '20 at 09:08

1 Answers1

0

Besides using a Timer, you can try to achieve it via Thread. Here is a demo maybe you can refer to. Using Thread.Sleep to simulate Timer.Interval.

private void button1_Click(object sender, EventArgs e)
{
    Rollbar.Minimum = 0;
    Rollbar.Maximum = 1000;

    Thread thread = new Thread(new ThreadStart((delegate
    {
        for (int i = 1; i <= 1000; i++)
        {
            Rollbar.Invoke(new MethodInvoker(delegate { Rollbar.Value = i; }));
            Thread.Sleep(100);
        }
    })));
    thread.Start();
}

Update: Using Task

private void button1_Click(object sender, EventArgs e)
{
    Rollbar.Minimum = 0;
    Rollbar.Maximum = 1000;

    var task = new Task(() =>
    {
        for (int i = 1; i <= 1000; i++)
        {
            Rollbar.Invoke(new MethodInvoker(delegate { Rollbar.Value = i; }));
            Thread.Sleep(100);
        }
    });
    task.Start();
    task.ContinueWith(t => Reset());
}

void Reset()
{
    Thread.Sleep(1000);
    Rollbar.Invoke(new MethodInvoker(delegate { Rollbar.Value = 0; }));
}
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37
  • thanks, this works, however i do not know how i would reset the progress bar after it reaches the maximum? – Rafi Nonato Mar 30 '20 at 08:23
  • If you want to create a continuation when ProcessBar reaches the maximum. You can try `Task` with the method `Task.ContinueWith`. You can refer to my latest reply. – 大陸北方網友 Mar 30 '20 at 08:58