Windows Develop Bookmark and Share   
 index > Windows Forms General > ProgressBar Value
 

ProgressBar Value

Hello Everyone,

I know this topic has been discussed over and over again, but I still don't get it. I've downloaded an example program that lets you encrypt a file using an RSA key. The original program worked in a thread, updating the ProgressBar in the encryption thread, but when running it I get a Cross-thread error. According to forums that I've searched through, you should use Invoke and such to update the UI thread. That, being I'm pretty new at C#, is too advanced for me at this point.

So I've done a lot of trial and error these past days, but just can't figure it out. The actual program works, if I do the encryption part in the main program, but the result is that the application "appears"to be freezing up during the encryption progress. At this point, I've managed to get it to work, using a background worker. That is apparently the easiest way, if I understand it correctly, to deal with a time consuming task when not using threading.

So, I've put the ProgressBar update into the BackgroundWorker's ProgressChangedEvent part. But I don't know what value to put into the DoWork part, so it can update correctly. This is the program code I'm using:

Code Snippet

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)

{

try

{

System.IO.FileInfo finfo = new System.IO.FileInfo(inFile);

// This is where I need the data from the fileinfo? (x)

// Original code example had progressBar.Maximum = (int)(finfo.Length / 32);

System.IO.FileStream fout = System.IO.File.Open(outFile, System.IO.FileMode.Create, System.IO.FileAccess.Write, System.IO.FileShare.Write);

System.IO.FileStream fin = System.IO.File.Open(inFile, System.IO.FileMode.Open, System.IO.FileAccess.Read, System.IO.FileShare.Read);

byte[] buffer = new byte[32];

byte[] encbuffer = new byte[32];

int c = 0;

while ((c = fin.Read(buffer, 0, 32)) > 0)

{

if (c != buffer.Length)

{

byte[] buffer2 = new byte[c];

for (int i = 0; i < c; i++)

buffer2[i] = buffer[i];

encbuffer = rsa.Encrypt(buffer2, false);

fout.Write(encbuffer, 0, encbuffer.Length);

}

else

{

encbuffer = rsa.Encrypt(buffer, false);

fout.Write(encbuffer, 0, encbuffer.Length);

}

backgroundWorker1.ReportProgress(x); // this is where I need 'x'

}

fin.Close();

fout.Close();

}

catch (Exception ex)

{

MessageBox.Show("Error: " + ex.Message, "Error");

}

}

I've tried to set the 'x' (int) to the value (int)(finfo.Length / 32), but it only gives me errors. I also get errors stating that the Value is incorrect, it needs to be between Minimum and Maximum Value. So how do I obtain the correct value, so it updates theProgressBar correctly? Anyoneunderstand what I mean, andmaybe be able to show me what I've done wrong? Any help is appreciated.

Thank You,

ScIeNcE_ErRoR

ScIeNcE_ErRoR  Sunday, September 02, 2007 4:24 PM
int c = 0;
long byteCnt = 0;
long byteMax = finfo.Length;
int progress = -1;
while ((c = fin.Read(buffer, 0, 32)) > 0) {
byteCnt += c;
...
int p = (int)(100 * byteCnt / byteMax)
if (p != progress) {
progress = p;
backgroundWorker1.ReportProgress(progress);
}
}

The progress variable ensures you don't call ReportProgress too often.
nobugz  Monday, September 03, 2007 2:52 PM
int c = 0;
long byteCnt = 0;
long byteMax = finfo.Length;
int progress = -1;
while ((c = fin.Read(buffer, 0, 32)) > 0) {
byteCnt += c;
...
int p = (int)(100 * byteCnt / byteMax)
if (p != progress) {
progress = p;
backgroundWorker1.ReportProgress(progress);
}
}

The progress variable ensures you don't call ReportProgress too often.
nobugz  Monday, September 03, 2007 2:52 PM

Thank you very much, I really appreciated your help. I've been at it for days now and haven't been able to figure it out. It was nice to see all the program code work without any Cross Thread Errors, Value Errors, and "freezing" UI. Thanks again.

ScIeNcE_ErRoR  Monday, September 03, 2007 3:31 PM

You can use google to search for other answers

Custom Search

More Threads

• C# Difference between "this" and "f1" in Form1 f1 = new Form1();
• Web Browser Control question!
• Users
• Combo Box Trouble
• Resgistering event handler with dynamically generated linklabels
• WebBrowser disappears when docking its parent window
• creating label
• I am unable to provide persistence to drawing a line using Mouse at runtime.
• list view set focus to item
• Use Controls from a different thread ?