Mallory Rich Mallory Rich - 1 month ago 9
C# Question

WDF Progress bar incrementing by odd number and looping

I have a progress bar with a min of 0 and max of 100. If I increment it by 33 it will end at 99/100 but then when I loop it it tries to add 33 to the 99 and I get Out of Bounds exception even though I set the value of the progress bar back to 0 after every loop. Can anyone suggest a solution around this?

private void WorkProgress1(int progress)
{

pbProgress.Value = progress;
}

private void DoWork()
{

if (_progress == 100) { _progress = 0; }
int Increment = (100 / count);
_progress += (Increment);
WorkProgress1(_progress);

}

Answer

A very simple solution would be to check if _progress + increment is more than 100 before you assign the value to _progress. If it is equal or more than 100 send 100 to WorkProgress1() and put _progress back to 0.

private void DoWork()
{
   int increment = (100 / count);

   if(_progress + increment >= 100)
   {
      WorkProgress1(100);
      _progress = 0;
   }
   else
   {
      _progress += (increment);
      WorkProgress1(_progress);
   }
}
Comments