ProgressBar Not Updating Question

N

needin4mation

Please consider:

foreach (ListViewItem item in listViewFiles.Items)
{
// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.
pBar1.Maximum = 4; //filenames.Length;
// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file being
copied.
pBar1.Step = 1;

string source=item.SubItems[0].Text.Trim();
string destination=item.SubItems[1].Text.Trim();
lisInfoList.Items.Add("Copying: " + source + " to " + destination);
File.Copy(source,destination,true);
item.SubItems[2].Text="Success";
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
Application.DoEvents();
}

I set the Maximum to 4 because I know I only have 4 files for testing.
I can see the progressbar update very quickly to halfway on two of the
small files. The two larger 36MB files do not update the bar at all
until the end and then the blue bars on the ProgressBar only go up to
halfway when completed. What I am trying to do is have it so that each
file copied gets the ProgressBar a little closer to the end until they
are all copied. Thank you for any help.
 
R

Rolf Molini

Hi,

your application starts the copying processes and the display of the progess-bar from within the same thread. File-IO can be very
consumptive regarding processor-time and thus performance. I think your application is too busy with copying (especially in the case
of large files) and thus the ProgressBar is not updated.

I had a similar problem with writing video files (up to serveral hundred MBytes in size) and displaying progress in a separate form.
Only if I put the writing in a separate thread the ProgressBar in my popup-form would update correctly.

If you'll use a separate thread created from the main application for coyping, let the copying-thread call a "callback-function"
defined within the calling thread (the main application) so that the outside world of the copying-thread keeps being informed on how
far copying has proceeded.

This callback-function should then be able to update the ProgressBar.


On the other hand, just for a try:

pBar1.PerformStep();

pBar1.Refresh();


Regards
Rolf
 
J

Jon Skeet [C# MVP]

If I do the other thread, does that mean it cannot be on the same form?

No, it just means you can't update the form directly from that thread.
You have to use Control.Invoke/BeginInvoke to marshal UI updates to the
UI thread.
 
N

needin4mation

I was able to do the following:

// Display the ProgressBar control.
pBar1.Visible = true;
// Set Minimum to 1 to represent the first file being copied.
pBar1.Minimum = 1;
// Set Maximum to the total number of files to copy.

// Set the initial value of the ProgressBar.
pBar1.Value = 1;
// Set the Step property to a value of 1 to represent each file being
copied.
pBar1.Step = 1;

try
{
foreach (ListViewItem item in listViewFiles.Items)
{
pBar1.Maximum = listViewFiles.Items.Count;
try
{
item.SubItems[2].Text="Copying...";
Application.DoEvents();
string source=item.SubItems[0].Text.Trim();
string destination=item.SubItems[1].Text.Trim();
lisInfoList.Items.Add("Copying: " + source + " to " + destination);
File.Copy(source,destination,true);
item.SubItems[2].Text="Success";
Application.DoEvents();
// Perform the increment on the ProgressBar.
pBar1.PerformStep();
Application.DoEvents();

}
....

And the progressbar updates based upon the number of files I have. But
now that I think about it that's not really what I meant to do. I was
thinking I could make the progressbar update based upon how far along
the file was during the copy. So as say a 14MB file is copying it does
like windows and moves the bar until 14MB is reached. Not sure what to
calculate though. What I have above is kind of an "overall"
progressbar. I don't know if using DoEvents() is a good thing either.
 
R

Rolf Molini

In such cases (copying, writing several files) I use to update the ProgressBar according to the file size of the individual file
related to the overall size of all files.

If you want to go even deeper you would have to read a source-file block-wise into a buffer and write from that buffer blockwise to
the target file. The size of the buffer (1KB, 1 MB, ...) defines the granularity of the ProgressBar-information.

Regards

Rolf
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top