2014-10-03 4 views
-1

성능 데이터를 표시하는 폼을 수행 중이며 프로세서와 메모리에 대한 몇 가지 정보를 표시하는 몇 가지 성능 카운터가 있습니다. 첫 번째 결과는 항상 "0"이므로 다음 값을 할당하기 전에 절전 스레드를 수행하고 있습니다. 이렇게함으로써 문제는 실제로 프로그램을 느리게 만드는 것입니다.PerformanceCounters를 코딩하는 가장 좋은 방법

심지어 창을 이동하는 속도가 느리며 기본적으로 다른 두 번째 실행중인 스레드 잠자기 이벤트로 인해 내기. 타이머를 항상 1 초 간격으로 활성화하도록 설정 했으므로 "실시간"정보를 표시해야합니다.

private PerformanceCounter pcProcess; //Process 
    private PerformanceCounter pcMemory; //Memory 

    private void tmrProcess_Tick(System.Object sender, System.EventArgs e) 
    { 
     pcProcess = new PerformanceCounter(); //New Performance Counter Object 
     pcProcess.CategoryName = "Processor"; //Specify Process Counter 
     pcProcess.CounterName = "Interrupts/sec"; 
     pcProcess.InstanceName = "_Total"; 
     pcProcess.NextValue(); 
     System.Threading.Thread.Sleep(1000); 
     cpInt.Text = pcProcess.NextValue().ToString(); //Display 
     pcProcess.CounterName = "% Processor Time"; 
     pcProcess.InstanceName = "_Total"; 
     pcProcess.NextValue(); 
     System.Threading.Thread.Sleep(1000); 
     cpPower.Text = pcProcess.NextValue().ToString() + "%"; //Display 
    } 

    private void tmrMemory_Tick(System.Object sender, System.EventArgs e) 
    { 
     pcMemory = new PerformanceCounter(); 

     pcMemory.CategoryName = "Memory"; 
     //This counter gives a general idea of how many times information being requested is not   where the application (and VMM) expects it to be 
     pcMemory.CounterName = "Available MBytes"; 
     avlMem.Text = pcMemory.NextValue().ToString() + " Mb"; 
     pcMemory.CounterName = "Page Faults/sec"; 
     pcMemory.NextValue(); 
     System.Threading.Thread.Sleep(1000); 
     pgFaults.Text = pcMemory.NextValue().ToString(); 
    } 

답변

1
  • 드롭 Thread.Sleep를 호출의 아이디어 :

    여기에 지금까지 내 코드입니다. 그것은 거의 결코 좋은 생각입니다.
  • 각 타이머 틱마다 PerformanceCounter()의 새 인스턴스를 만들지 말고 perfmonCounter.NextValue()으로 전화하십시오.
  • 카운터 초기화 후 NextValue()을 한 번 호출하면 첫 번째 타이머 틱에서 0.0 이상을 반환합니다. http://msdn.microsoft.com/en-us/library/system.diagnostics.performancecounter.nextvalue(v=vs.110).aspxremarks 섹션 참조 :

를 카운터의 산출 값이 카운터 판독에 따라 두 가지 경우 제 1 판독 동작이 0.0를 반환한다. 성능 카운터를 다시 설정하면 다른 카운터를 지정하는 속성은 새 성능 카운터를 만드는 것과 동일하며 새 속성을 사용하는 첫 번째 읽기 작업은 0.0을 반환합니다. NextValue 메서드 호출 사이의 권장 지연 시간은 카운터가 다음 증분 읽기 을 수행 할 수 있도록하는 1 초입니다.

그래서 기본적으로 :

private PerformanceCounter pcMemory; 

private void InitPerfmon() 
{ 
    this.pcMemory = new PerformanceCounter(); 
    this.pcMemory.CounterName = "Available MBytes"; 
    this.pcMemory..... 
    ... 
    this.pcMemory.NextValue(); 
} 

private void tmrMemory_Tick(System.Object sender, System.EventArgs e) 
{ 
    this.pgFaults.Text = this.pcMemory.NextValue().ToString(); 
} 
+0

내가 performancecounter 두 번 전화를해야할까요? 그래서 this.pcMemory = new PerformanceCounter this.pcProcess = new PerformanceCounter(); – user263029

+0

@ user263029 예, 모니터링 할 각 카운터에 대해 하나의 인스턴스가 있어야합니다. – ken2k

관련 문제