[英]How can I report multiple reportprogress values from backgroundworker dowork event to a dataGridView?
添加了此类:
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);
}
}
}
}
最后是backgroundworker
progresschanged
事件,在这里我想将每个进程的值添加到单元格3下的datagridview1
行中。
private void backgroundWorker1_ProgressChanged(object sender, ProgressChangedEventArgs e)
{
foreach (MyProgress p in (e.UserState as List<MyProgress>))
{
currentRow.Cells[3].Value = p.Progress;
dataGridView1.Rows.Add(
p.Progress);
}
}
变量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;
}
}
但是我在foreach上遇到了异常:异常是:
Additional information: Collection was modified; enumeration operation may not execute.
System.InvalidOperationException was unhandled by user code
HResult=-2146233079
Message=Collection was modified; enumeration operation may not execute.
Source=mscorlib
StackTrace:
at System.Collections.Generic.List`1.Enumerator.MoveNextRare()
at Automation.Form1.backgroundWorker1_ProgressChanged(Object sender, ProgressChangedEventArgs e) in d:\C-Sharp\Automation\Automation\Automation\Form1.cs:line 719
InnerException:
据我了解您的问题,您需要这个
Integer rowIdx = dataGridView1.Rows.Add();
dataGridView1.Rows[rowIdx].Cells[3].Value = p.Progress
为什么会出错? -这是因为您有设计缺陷-在backgroundWorker1_DoWork
,它在后台线程上运行,您仍在修改collection,同时将其传递给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;
}
}
我不确定整个设计,因此我不打算对其进行更改。 只是说现在出了什么问题,而不是总的来说。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.