Question

I am using a background thread to run a stopwatch which I am using on a label and to increment a progressbar. The timer seems to be keeping time accurately but the progress bar isn't hitting 100% when I expect it to.

I am trying to use both to time 35 seconds and it hits 100% at nearly 36s, can anyone tell me why?

The Maximum value of the progress bar is set to 350, value is set to 0.

private int currentScore = 0;

Stopwatch swTimer = new Stopwatch();

private void backgroundWorkerTimer_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
    TimeSpan elapsedTime = swTimer.Elapsed;
    labelTime.Text = String.Format("{0}.{1:%f}", elapsedTime.Seconds, elapsedTime);
    progressBarTimer.Increment(1);
}

private void backgroundWorkerTimer_DoWork(object sender, DoWorkEventArgs e)
{
    while (true)
    {
        backgroundWorkerTimer.ReportProgress(0);
        System.Threading.Thread.Sleep(100);
    }            
}
Was it helpful?

Solution

This seems to have dried up so I will close it. I ended up making the progressBar max 345 and it seems to hit the end at about 35s, I am only using it for a visual representation of time left anyway.

Would be interested to know why 350 as a maximum isn't accurate though when labelTime is keeping up.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top