界面如上图(vs2005):

先添加组件performanceCounter,和timer, 设置好属性,一般是三个属性设置,

CategorgName ,CounterName,和InstanceName。具体可以查查csdn文档。

一般用到的命名空间是:using System.Diagnostics;

 

代码如下:

     private void Form1_Load(object sender, EventArgs e)
           {
               this.timer1.Enabled = true;

            
           }

           private void timer1_Tick(object sender, EventArgs e)
           {     //cpu
              Int32 cpuCounter=(Int32)this.performanceCounter1.NextValue();
              this.label2.Text = cpuCounter.ToString()+"%";
           
               //cpu_bar
              this.progressBar1.Value = cpuCounter;

           

                //memory
              int memoryCounter =(int)this.performanceCounter2.NextValue();
              this.label4.Text = memoryCounter.ToString()+"MB";

            

              //sever session

 

Performance Counter的使用误区

2008-07-11 20:42

很多人在使用PerfomanceCounter的时候直接new PerfomanceCounter实例,然后就去调用NextValue()方法。这样往往得到的值是0.00,今天我也犯了这么错误,找个了半天,终于发现,performance counter在计算值得时候,需要两个样本,如果我们获取到PerformanceCounter后直接调用NextValue()方法,则只会获取到第一个样本的值,该值往往会是0。

下面告诉大家正确的代码是:
PerformanceCounter pc = new PerformanceCounter();
            pc.CategoryName = cataName;
            pc.CounterName = counter;
            pc.InstanceName = instance;
            pc.MachineName = ".";
            pc.ReadOnly = true;
           
           pc.NextValue();
            System.Threading.Thread.Sleep(1000); //等1秒,让后系统获取下一个样本

           return pc.NextValue();

其实,如果你的对象不销毁,下次再获取的时候就不会为0了,也就不需要再sleep(1000),只要你两次调用NextValue的时间间隔大于1秒。

 

 

下面是一个简单的实现 操作自定义window performance counter的实例。在运行程序的过程中我们通过操作系统的performance面板查看或者写log文件,对我们应用程序的性能进行监视,分析。将有助于我们分析解决系统的性能等问题。

 

代码功能: 

1,实现添加一个counter 类别

2,添加一个或者多个counter对象

3,获取counter对象,并赋值。

 

在系统的performance 面板中查看。效果如下图:

 

代码:

using System;
using System.Collections.Generic;
using System.Text;
using System.Threading;
using System.Diagnostics;

namespace WritePerformanceLog
{
    
class Program
    {
        
static void Main(string[] args)
        {
            
if (!PerformanceCounterCategory.Exists("test_Category_1"))
            {
                
// Create a collection of type CounterCreationDataCollection.
                System.Diagnostics.CounterCreationDataCollection CounterDatas =
                   
new System.Diagnostics.CounterCreationDataCollection();
                
// Create the counters and set their properties.
                System.Diagnostics.CounterCreationData cdCounter1 =
                   
new System.Diagnostics.CounterCreationData();
                System.Diagnostics.CounterCreationData cdCounter2 =
                   
new System.Diagnostics.CounterCreationData();
                cdCounter1.CounterName = 
"Counter1";
                cdCounter1.CounterHelp = 
"help string1";
                cdCounter1.CounterType = System.Diagnostics.PerformanceCounterType.NumberOfItems64;
                cdCounter2.CounterName = 
"Counter2";
                cdCounter2.CounterHelp = 
"help string 2";
                cdCounter2.CounterType = System.Diagnostics.PerformanceCounterType.NumberOfItems64;

                
                
// Add both counters to the collection.
                CounterDatas.Add(cdCounter1);
                CounterDatas.Add(cdCounter2);
                
// Create the category and pass the collection to it.
                System.Diagnostics.PerformanceCounterCategory.Create(
                   
"test_Category_1""Category help", CounterDatas);
            }
            
else
            {

                PerformanceCounter cdCounter1 = 
new PerformanceCounter("test_Category_1""Counter1"false);
                PerformanceCounter cdCounter2 = 
new PerformanceCounter("test_Category_1""Counter2"false);
                
                cdCounter1.ReadOnly = 
false;

                
forint i=0;i<10000;i++ )
                {
                    cdCounter1.RawValue = i;
                    
//cdCounter1.Increment();

                    cdCounter2.RawValue = i+
1;

                    Thread.Sleep(
100);
                }
                                           

                Console.WriteLine(cdCounter1.NextValue());
                Console.WriteLine(cdCounter2.RawValue);
                Console.WriteLine(cdCounter1.NextSample());


                Console.Read();

            }
        }
    }
}