通过0ne backgroundworker更新一些进度条 [英] Update few progress bars by 0ne backgroundworker

查看:76
本文介绍了通过0ne backgroundworker更新一些进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

我创建了一些程序化的进度条并尝试从一个后台工作者更新...

但是只更新了一个创建的进度条。我的程序复制文件。所有文件复制到哪里,但我看到进度仅在最后一个(进度条)中更改。需要帮忙。谢谢。

public void startCopyFile(string srcFile,string destFile)

{



FileToCopy = srcFile;

CopyToDest = destFile;

//日期 - 时间

标签lbdatetime =新标签();

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);

//文件名

标签lbfilename = new Label();

lbfilename.Text = FileToCopy;

lbfilename.Size = new大小(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);

//进度条

bar = new ProgressBar();

bar.Name = name_.ToString();

bar.Location = new Point(FileToCopy.PadRight(400).Length + 180,y_step);

bar.Size = new尺寸(115,15);

panelProgress.Controls.Add(bar);

panelProgress.Controls.Add(bar);

//百分比

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 =新字体(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);



}

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);

}

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





}



}

推荐答案

自每次创建进度条时都使用相同的变量 bar ,如果进度发生变化,您希望如何更新相应的条形Handler将只看到<$的当前值c $ c> bar 。



一种可能性是创建一个进度条数组(例如列出< ProgressBar> )以便将它们编入索引。然后,您可以使用user参数调用 ReportProgress 时传递该信息,然后您可以在Handler中解码以确定要更新的进度条。



或者更好的是,创建一个包含一组控件的用户控件,并为每个副本创建一个控件(然后可以使用堆叠容器来布局控件)。这样,每个线程都会与自己的用户控件相关联。



另一方面,如果副本数量很少(限制为2或3份副本)在一次),你可能只是为每个后台工作者使用不同的处理程序...



顺便说一下,如果你需要比后台更多的进度条,你的设计是有问题的工人。它使代码更不可重用,更难维护。



我认为,您应考虑应用单一责任原则(参见前文 http://en.wikipedia.org/wiki/Single_responsibility_principle [ ^ ]。)



无论如何,如果这真的是你想要的并且你有一个处理程序,你总是可以发送报告进度时的一些用户信息,以便您知道要更新的栏。
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.


这篇关于通过0ne backgroundworker更新一些进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆