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?