C# 如何将backgroundworker dowork事件中的多个reportprogress值报告给dataGridView?
添加了此类:C# 如何将backgroundworker dowork事件中的多个reportprogress值报告给dataGridView?,c#,.net,winforms,C#,.net,Winforms,添加了此类: public class MyProgress { public string Id { get; set; } public string Progress { get; set; } } 嫁妆活动: private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e) {
public class MyProgress
{
public string Id { get; set; }
public string Progress { get; set; }
}
嫁妆活动:
private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
{
BackgroundWorker worker = sender as BackgroundWorker;
if ((worker.CancellationPending == true))
{
e.Cancel = true;
}
else
{
List<IntPtr> intptrs = GetProcessesIntptrList();
for (int x = 0; x < intptrs.Count ; x++)
{
GetProcessInfo(intptrs[x]);
}
while (true)
{
List<MyProgress> prog = new List<MyProgress>();
prog = new List<MyProgress>();
procList = Process.GetProcesses().ToList();
for (int i = 0; i < procList.Count; i++)
{
Process[] processes = Process.GetProcessesByName(procList[i].ProcessName);
PerformanceCounter performanceCounter = new PerformanceCounter();
performanceCounter.CategoryName = "Process";
performanceCounter.CounterName = "Working Set - Private";//"Working Set";
performanceCounter.InstanceName = processes[0].ProcessName;
prog.Add(new MyProgress { Id = procList[i].ProcessName, Progress = ((uint)performanceCounter.NextValue() / 1024).ToString("N0") });
worker.ReportProgress(0, prog);
}
}
}
}
变量currentRow尚不存在。我知道单元格3是我想要添加所有进程行值的地方
我也不知道应该有多少行。
以及如何向单元格3下的行报告每个进程值
我在progresschanged事件中尝试了这一点:
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
foreach (MyProgress p in (e.UserState as List<MyProgress>))
{
int rowIdx = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIdx].Cells[3].Value = p.Progress;
}
}
就我理解你的问题而言,你需要这个
Integer rowIdx = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIdx].Cells[3].Value = p.Progress
为什么会出错这是因为您有设计缺陷-在后台线程上运行的backgroundWorker1\u-DoWork
中,您仍在修改集合,同时将集合传递给progress
事件。当您在一个线程上迭代时,另一个线程会添加项目。您需要做的是创建一个副本,传递此副本并迭代此副本。下面是一种使用数组执行此操作的简单方法
. . . .
MyProgress[] arrP;
prog.CopyTo(arrP);
worker.ReportProgress(0, arrP);
. . .
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
MyProgress[] progArr = (MyProgress[])e.UserState;
foreach (MyProgress p in progArr)
{
int rowIdx = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIdx].Cells[3].Value = p.Progress;
}
}
我不确定整个设计,所以我不想改变它。现在只说问题所在,而不是一般情况。您应该从GUI线程更新GUI。请参阅在GUI线程上运行@Tarik
ProgressChanged
事件如果currentRow不存在,请创建一行。我的意思是,添加一个if语句来检查行是否不存在并创建它。这段代码中有很多bug。现在导致程序崩溃的是ReportProgress()调用,它必须移动到for()循环之外。现在,您将要处理下一个bug,每秒添加数千行将无法正常工作。取消也无法工作。T.S我在foreach上的progresschanged事件中遇到异常,在p上的异常:附加信息:集合已修改;枚举操作不能执行。
Integer rowIdx = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIdx].Cells[3].Value = p.Progress
. . . .
MyProgress[] arrP;
prog.CopyTo(arrP);
worker.ReportProgress(0, arrP);
. . .
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
MyProgress[] progArr = (MyProgress[])e.UserState;
foreach (MyProgress p in progArr)
{
int rowIdx = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIdx].Cells[3].Value = p.Progress;
}
}