如何更快地显示所有进程的内存使用情况?

时间:2015-07-13 18:27:25

标签: c# .net winforms

问题是我需要遍历所有进程。 例如,在任务管理器中,它看起来像是每秒或更短时间内更新所有进程内存使用情况。

我有一个背景工作者dowork事件:

private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            BackgroundWorker worker = sender as BackgroundWorker;
            while (true)
            {
                completed = true;
                if ((worker.CancellationPending == true))
                {
                    e.Cancel = true;
                    break;
                }
                else
                {
                  GetProcessesMemoryUsage();
                }
            }
        }

方法GetProcessesMemoryUsage:

private void GetProcessesMemoryUsage()
{

 foreach (Process p in Process.GetProcesses())
            {
              if (File.Exists(p.MainModule.FileName))
                    {
                     Process[] processes = Process.GetProcessesByName(p.ProcessName);

                        PerformanceCounter performanceCounter = new PerformanceCounter();
                        performanceCounter.CategoryName = "Process";
                        performanceCounter.CounterName = "Working Set";
                        performanceCounter.InstanceName = processes[0].ProcessName;
                        memoryUsage = ((uint)performanceCounter.NextValue() / 1024).ToString("N0");
                    }
           }

需要让foreach循环来更新每个进程的内存使用情况。

然后它如何在任务管理器中工作?他们如何快速更新所有流程?

0 个答案:

没有答案