并行获取所有进程

时间:2011-05-06 10:01:00

标签: c# windows task-parallel-library

我正在尝试使用C#的TPL并行获取所有进程的CPU%。我的代码是:

        private IDictionary<Process, int> _usage = new Dictionary<Process, int>();

        public ProcessCpuUsageGetter()
        {            
            Process[] processes = Process.GetProcesses();
            int processCount = processes.Count();
            Task[] tasks = new Task[processCount];

            int counter = 0;
            for (int i = 0; i < processCount; i++)
            {
                tasks[i] = Task.Factory.StartNew(() => DoWork(processes[i]));
            }

            Task.WaitAll(tasks);
        }

        private void DoWork(object o)
        {
            Process process = (Process)o;
            PerformanceCounter pc = new PerformanceCounter("Process", "% Processor Time", process.ProcessName, true);
            pc.NextValue();
            Thread.Sleep(1000);
            int cpuPercent = (int)pc.NextValue() / Environment.ProcessorCount;
            _usage.Add(process, cpuPercent);
        }

但它失败了An item with the same key has already been added.关于我做错的任何想法?

1 个答案:

答案 0 :(得分:6)

当传递给用于启动任务的表达式时,问题是局部变量i闭包。即使DoWork(processes[i])修改了i,这也会导致for使用的i的当前值。

创建一个局部变量:

for (int i = 0; i < processCount; i++)
{
     int localI = i;
     tasks[i] = Task.Factory.StartNew(() => DoWork(processes[localI]));
}