Search code examples
c#.netwpfprogress-bardispatchertimer

C# progressBar not updating until half progress complete


I am using a dispatcherTimer to update the progressBar's Value:

DispatcherTimer tim = new DispatcherTimer();
tim.Interval = TimeSpan.FromMilliseconds(100);
tim.Tick += delegate
{
    if (valuee < max)
    {
        prog.Value = valuee;
    }
    else
    {
        tim.Stop();
    }
};

And setting the progressBar's maximum value like this:

using (FileStream fs = File.Open(Filename, FileMode.Open))
{
    if (fs.Length > 10485760 && fs.Length <= 209715200)
        prog.Maximum = fs.Length / 1024;
    else if (fs.Length <= 10485760)
        prog.Maximum = fs.Length / 16;
    else if(fs.Length > 209715200)
        prog.Maximum = fs.Length / 1048576;
}

Then increasing the value after every iteration of the for loop:

var Input = File.OpenRead(path);
int Size = MainWindow.prog.Maximum;
for (long i = 0; i < Input.Length; i += Size)
{
    MainWindow.valuee++;
    PasswordEnter.valuee++;
    byte[] chunkData = new byte[chunkSize];
    int bytesRead = 0;
    while ((bytesRead = fsInput.Read(chunkData, 0, chunkSize)) > 0)
    {
        if (bytesRead != chunkSize)
        {
            for (int x = bytesRead - 1; x < chunkSize; x++)
            {
                chunkData[x] = 0;
            }
        }
        cryptoStream.Write(chunkData, 0, bytesRead);
    }
}

EDIT: This is how I am calling the AESCryptography class:

BackgroundWorker worker = new BackgroundWorker();
worker.DoWork += (obj, l) => lo.Encrypt(Filename, password.Password, "main");
worker.RunWorkerAsync();

But for some reason the progressBar doesn't update until more than half the iterations are over! what's wrong?


Solution

  • Your looping code opens a file, then inside the for loop increments the progress (valuee). It appears that the while loop reads to end-of-file. The for loop then keeps having the while make sure that it is still at EOF since no new file is opened. (I'm assuming that Input and fsInput are supposed to be the same variable.)

    The ReportProgress/ProgressChanged mechanism on the BackgroundWorker thread is a reliable way to handle the cross-thread intricacies of updating the UI from a background worker thread. You can use UserState to pass additional information, e.g. the name of the file currently being processed to display in a label.