469,322 Members | 1,336 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,322 developers. It's quick & easy.

ProgressBar Not Updating Question

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.

Nov 17 '05 #1
8 19134
I read something about doing this in another thread. Maybe someone
knows?

Nov 17 '05 #2
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
<ne***********@gmail.com> schrieb im Newsbeitrag news:11**********************@g14g2000cwa.googlegr oups.com...
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.

Nov 17 '05 #3
If I do the other thread, does that mean it cannot be on the same form?

Nov 17 '05 #4
<ne***********@gmail.com> wrote:
I read something about doing this in another thread. Maybe someone
knows?


You should definitely be performing long-running operations in a worker
thread - but then you can't update the UI directly from the worker
thread.

See http://www.pobox.com/~skeet/csharp/t...winforms.shtml for more
information.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #5
<ne***********@gmail.com> wrote:
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.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 17 '05 #6
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.

Nov 17 '05 #7
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


<ne***********@gmail.com> schrieb im Newsbeitrag news:11**********************@f14g2000cwb.googlegr oups.com...
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.


Nov 17 '05 #8
I have an article on my blog where I use some Windows API's to show a
progress bar just like Windows does. Hope this helps:

http://khsw.blogspot.com/2005/08/cop...-in-vbnet.html

--
http://www.khsw-be.net
"ne***********@gmail.com" wrote:
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.

Nov 17 '05 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Mayolo Juarez via DotNetMonster.com | last post: by
3 posts views Thread by Stefan Turalski \(stic\) | last post: by
3 posts views Thread by Steve Teeples | last post: by
3 posts views Thread by al jones | last post: by
2 posts views Thread by =?Utf-8?B?QWFyb24=?= | last post: by
1 post views Thread by CARIGAR | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by Gurmeet2796 | last post: by
reply views Thread by mdpf | last post: by
reply views Thread by harlem98 | last post: by
reply views Thread by listenups61195 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.