Click here to Skip to main content
15,893,594 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using a similar code:

C#
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
        {
            progressBar1.Value = e.ProgressPercentage;
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            MessageBox.Show("Complete");
        }


        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {

            int[] arr = new int[21];
            for (int i = 1; i <= 20; i++)
            {
                arr[i] = i;
                Thread.Sleep(100);
                int m = (i/arr.Length)*100;
                backgroundWorker1.ReportProgress(i*m);
            }
        }


The progress bar isn't updating. But if I replace m by 5 the progress is seen. Please help.. In the original program I have to use the above approach. Thanks. :)
Posted

Integer division is causing it;
C#
int m = (i/arr.Length)*100;


i divided by arr.lenght will always be zero.

Change it to
C#
int m = (int)((i/(double)arr.Length)*100);

And then set the progress to;
backgroundWorker1.ReportProgress(m);

Hope this helps,
Fredrik
 
Share this answer
 
v2
Comments
Tejas Shastri 17-Oct-14 4:15am    
Runtime error: Value of '115' is not valid for 'Value'. 'Value' should be between 'minimum' and 'maximum'.
Fredrik Bornander 17-Oct-14 4:22am    
Your ReportProgress should be set to m, I updated my answer.
Tejas Shastri 17-Oct-14 4:39am    
oh.. foolish mistake.. my bad.. Thanks for the help
Tejas Shastri 17-Oct-14 4:42am    
but is there a way to provide float value to progress.value??
Fredrik Bornander 17-Oct-14 8:05am    
No. And there's no point, just take your float value and multiply it by 100 (provided it's clamped to 0<=value<=1).
Um...remember your integer math?
(integer, between 1 and 20 inclusive) / (integer, value 21) == 0

So...
C#
int m = (i/arr.Length)*100;
Always gives m of zero.
Try this:
C#
int m = (i * 100) / arr.Length;
 
Share this answer
 
Comments
Tejas Shastri 17-Oct-14 4:07am    
This give values greater than 100 to Value(as in Progressbar1.value).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900