I have a progress bar on my winform that is being updated from a call back method called from CopyFileEx.
The problem is the progress bar won’t update until it reaches 100%.
The progress bar percentage is updated from a background worker and is based the on the amount of bytes copied compared to the total amount of bytes of all files.
My code to calculate the amount of bytes is (I don’t think there are any problems here)
CopyFileCallbackAction myCallback(FileInfo source, FileInfo destination, object state, long totalFileSize, long totalBytesTransferred)
{
double dProgress = (totalBytesTransferred / byteCount) * 100.0;
backupWorker.ReportProgress((int)dProgress);
return CopyFileCallbackAction.Continue;
}
If I show a message box with the values for totalBytesTransferred and byteCount, and do the calculation myself it works fine (truncating any numbers after the decimal point – as it is cast to an int) and I get the percentage.
For some reason though it doesn’t work when it is passed to my progress bar, until it is 100% and then it does it all in one.
Any ideas?
You’re doing integer (actually
long) division, which returns0.You need to cast one operand to
double.