Click here to Skip to main content
15,889,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello,
I create few progress bars programmatic and try update from one background worker ...
But updated only one of created progress bar . My programm copy files . All files copied where but i see progress changed only in last one(progress bar). Need help. Thanks.
public void startCopyFile(string srcFile, string destFile)
{

FileToCopy = srcFile;
CopyToDest = destFile;
//Date - Time
Label lbdatetime = new Label();
lbdatetime.Text = DateTime.Now.ToLocalTime().ToString();
lbdatetime.Size = new Size(120, 20);
lbdatetime.Location = new Point(x_step, y_step + 1);
lbdatetime.Font = new Font("Arial", 9);
panelProgress.Controls.Add(lbdatetime);
//File name
Label lbfilename = new Label();
lbfilename.Text = FileToCopy;
lbfilename.Size = new Size(FileToCopy.PadRight(400).Length, 20);
lbfilename.Location = new Point(150, y_step);
lbfilename.Font = new Font("Arial", 10);
lbfilename.ForeColor = Color.BlueViolet;
panelProgress.Controls.Add(lbfilename);
//Progress bar
bar = new ProgressBar();
bar.Name = name_.ToString();
bar.Location = new Point(FileToCopy.PadRight(400).Length + 180, y_step);
bar.Size = new Size(115, 15);
panelProgress.Controls.Add(bar);
panelProgress.Controls.Add(bar);
//Percentage
lbper = new Label();
lbper.Text = FileToCopy;
lbper.Size = new Size(60, 20);
lbper.Location = new Point(FileToCopy.PadRight(700).Length + 20, y_step);
lbper.Font = new Font("Arial", 10);
lbper.ForeColor = Color.Green;
panelProgress.Controls.Add(lbper);
x_step = 10;
y_step += 20;
name_++;
mCopier = new BackgroundWorker();
mCopier.WorkerReportsProgress = true;

mCopier.DoWork += Copier_DoWork;
mCopier.RunWorkerCompleted += Copier_RunWorkerCompleted;
mCopier.WorkerSupportsCancellation = true;
mCopier.ProgressChanged += new ProgressChangedEventHandler(mCopier_ProgressChanged);
mCopier.RunWorkerAsync(this.bar.Name);

}
C#
private void mCopier_ProgressChanged(object sender, ProgressChangedEventArgs e)
       {
           bar.Value = e.ProgressPercentage;
           lbper.Text = e.ProgressPercentage.ToString() + "%"; bar.Value = e.ProgressPercentage;


}
Posted
Comments
BillWoodruff 18-Oct-14 22:47pm    
Are you saying that the ProgressBar changes, but the Label 'lbper is not updated ?

Note that you set the 'Value of 'bar twice in your ProgressChanged EventHandler, but that should not cause an error.

1 solution

Since every time you created a progress bar you use the same variable bar, how do you expect to update the appropriate bar as the progress changed Handler will see only the currect value of bar.

One possibilities would be to create an array of progress bar (for ex. List<ProgressBar>) so that they would be indexed. Then you can pass that information when calling ReportProgress using the user argument which you can then decode in the Handler to determine which progress bar to update.

Or better yet, create a user control that contains one set of control and create one for every copy (you can then uses a stacked container to layout controls). That way, each thread would be associated to its own user control.

On the other hand, if the number of copy is small (limited to say 2 or 3 copy at a time), you might simply uses distinct Handler for each background worker...

By the way, your design is questionnable if you need more progress bar than background worker. It make the code far less reusable and harder to maintain.

I think, it is case where you should consider to apply the single responsability principle (see for ex. http://en.wikipedia.org/wiki/Single_responsibility_principle[^].)

Anyway, if this is really what you want and you have a single handler, you can always send some user information when reporting progress so that you would know which bar to update.
 
Share this answer
 
Comments
BillWoodruff 19-Oct-14 9:05am    
+5 This is a very thoughtful answer with very wise advice.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900