Added this class:
public class MyProgress
{
public string Id { get; set; }
public string Progress { get; set; }
}
The dowork event:
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);
}
}
}
}
And last the backgroundworker
progresschanged
event where I want to add the values of each process to the datagridview1
rows under cell 3.
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);
}
}
The variable currentRow not exist yet. And I know that cell 3 is where I want to add all the rows of the processes values.
And I also don't know how many rows there should be. And how to do the reporting of each process value to a row under cell 3 ?
I tried this in the progresschanged event:
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;
}
}
But i'm getting exception on the foreach: The exception is:
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: