1

だから私はタスク マネージャーのクローンを作成しています。現在、これを使用して各プロセスの CPU 使用率 %s を計算しています。問題は、これが非常に遅いことです。これを高速化する方法があるかどうか疑問に思っていました。

また、PerformanceCounter のメソッドや WMI を使用することも許可されていません。

//Omitted:
// - Process[] processes just holds the currently running processes
// - rows[] is a list of the rows I have in a table which shows the tables data
// - rows[2] is the column for CPU usage
// - rows[0] is the column for PID
//========
//Do CPU usages
double totalCPUTime = 0;
foreach (Process p in processes)
{
    try
    {
        totalCPUTime += p.TotalProcessorTime.TotalMilliseconds;
    }
    catch (System.ComponentModel.Win32Exception e)
    {
        //Some processes do not give access rights to view their time.
    }
}
foreach (Process p in processes)
{
    double millis = 0;
    try
    {
        millis = p.TotalProcessorTime.TotalMilliseconds;
    }
    catch (System.ComponentModel.Win32Exception e)
    {
        //Some processes do not give access rights to view their time.
    }
    double pct = 100 * millis / totalCPUTime;
    for (int i = 0; i < rows.Count; i++)
    {
        if(rows[i].Cells[0].Value.Equals(p.Id))
        {
            rows[i].Cells[2].Value = pct;
            break;
        }
    }
}
4

1 に答える 1

2

PerformanceCounterコンポーネントを使用して、さまざまなパフォーマンス統計を取得します。

var cpuCounter = new PerformanceCounter("Process", "% Processor Time", p.ProcessName);
double value = cpuCounter.NextValue();    

そして、これがタスクマネージャーのようなものを作成するサンプルです。

WinAPI関数の使用も検討してくださいGetProcessTimes

于 2012-10-19T22:42:04.397 に答える