[C#] PerformanceCounter CPU 값이 이상하게 찍힙니다
조회수 1054회
PerformanceCounter을 이용하여 CPU 사용량을 확인하는 프로그램을 .NET으로 개발하고 있습니다. CPU 사용량을 확인하기 위해 아래 코드를 이용했습니다.
private int getCPUUsage()
{
PerformanceCounter cnt = new PerformanceCounter("Processor", "% Processor Time", "_Total");
return (int)cnt.NextValue();
}
그런데 함수의 리턴값이 0, 0, 0, 100, 0, 0 등으로 0과 100만 나옵니다. 현재 CPU 점유율이 12%밖에 안되도 0과 100이 간헐적으로 나옵니다.
그리고 신기한 점은
private int getCPUUsage()
{
PerformanceCounter cnt = new PerformanceCounter("Processor", "% Processor Time", "_Total");
MessageBox.Show(cnt.NextValue().ToString());
return (int)cnt.NextValue();
}
을 하면 메시지박스에는 0이 표시되고 리턴값은 정확합니다
그래서 함수 내에서 cnt.NextValue()
를 한번 더 실행해 봤지만 소용이 없었습니다.
어떻게 해야 정확한 CPU 점유율을 얻을 수 있나요?
-
(•́ ✖ •̀)
알 수 없는 사용자
댓글 입력