Brian Var
Brian Var

Reputation: 6227

How to bind a progress bar to an interval?

I've added a determinate progress bar to my app which is an interval training app. The timer starts for a set number of seconds then stop for a set number of seconds, then repeats this sequence for a set number of rounds, much like this kind off application:

http://www.windowsphone.com/en-ie/store/app/simple-training-timer/2f946ba9-ffd9-4f98-85a5-c67f97868ebe

This the declaration of the progress bar in my code:

ProgressBar  IsIndeterminate="False" x:Name="overallProgressBar" Maximum="100"  Width="200" Margin="114,214,142,288" FontWeight="Bold" Style="{StaticResource ProgressBarStyle2}"

And this is how I call set the progress bar to increment in the timer_tick:

        void myTimer_Tick(object sender, EventArgs e)
        {


                // update the textblock on the display
                // with hh, mm, ss, ms
                ms = myStopwatch.ElapsedMilliseconds;

                ss = ms / 1000;
                ms = ms % 1000;

                mm = ss / 60;
                ss = ss % 60;

                hh = mm / 60;
                mm = mm % 60;

                overallProgressBar.Value = ms;


                tblDisplay.Text = hh.ToString("00") + " : " +
                                  mm.ToString("00") + " : " +
                                  ss.ToString("00") + " : " +
                                  ms.ToString("000");



                //method to trigger timer stop and start at user input intervals
                intervalTrigger(ref workTm, ref restTm);


        }

At the moment this code just runs the progress bar to finish after about 5 seconds which is not in sync with the rounds being input.

I'm trying to set up my progress bar to run in sequence with these actions, do anyone know how I would achieve this?

Upvotes: 0

Views: 1719

Answers (2)

Peter Duniho
Peter Duniho

Reputation: 70671

What is the ProgressBar supposed to be representing exactly? Your question is not very clear.

Assuming a TimeSpan field named workTimeSpan that defines the duration of the interval being measured by the ProgressBar, and assuming you want to display the progress within that interval:

void myTimer_Tick(object sender, EventArgs e)
{
    overallProgressBar.Value =
        myStopwatch.ElapsedMilliseconds / workTimeSpan.TotalMilliseconds * 100;

    // update the textblock on the display
    // with hh, mm, ss, ms
    tblDisplay.Text = myStopwatch.Elapsed.ToString("hh:mm:ss.fff");

    //method to trigger timer stop and start at user input intervals
    intervalTrigger(ref workTm, ref restTm);
}

A better version in WPF would bind the control values to properties, but the above would work.

If the ProgressBar is supposed to represent something else, then please be specific about what it's supposed to represent, where that information is in your code, and how it's updated. See https://stackoverflow.com/help/mcve

Upvotes: 2

Robert Harvey
Robert Harvey

Reputation: 180808

Estimate a percentage of completion for each action, and then adjust the progress bar as a function of the overall percentage of completion each time a task completes.

Generally speaking, you can't create a determinate progress bar by using timers, since you have no way of knowing how long your operations are actually going to take.

Upvotes: 1

Related Questions