I've recently started working with multi-threaded Winform applications and have run up against a problem I don't understand.
Basically, when I try to have a background worker report its progress and that progress comes from a calculation, it is always zero.
//making background worker
BackgroundWorker myJanitor = new BackgroundWorker();
myJanitor.WorkerSupportsCancellation = true;
myJanitor.WorkerReportsProgress = true;
myJanitor.DoWork += new DoWorkEventHandler(cleanContactList);
myJanitor.ProgressChanged += new ProgressChangedEventHandler(myCleaningWorker_ProgressChanged);
myJanitor.RunWorkerAsync();
The 'cleanContactList' method loops through a DataGridView's rows, during which I try something like this:
int percentComplete = (myRow.Index / contactGridView.Rows .Count ) * 100;
(sender as BackgroundWorker).ReportProgress(percentComplete);
Frustratingly, percentComplete will ALWAYS be zero. If I debug it I can see that there is an actual calculation occurring (e.g. [2000/10000]*100) but the result is always zero.
What am I missing here? If I replace the calculation with, say, a Random.Next(0,100) call it updates fine.
Try index * 100/count. I think you're dividing two ints leading to 0 then multiplying by 100.
For example, this:
int index = 50;
int count = 100;
int percent = index/count * 100;
int percent2 = index * 100/count;
Console.WriteLine("{0} & {1}", percent, percent2);
Outputs 0 & 50.