Just another tech site

C# Performance Counters


public const string CounterCategoryName = "CounterName";
public const string CounterCategoryNameHelp = "Description";
public const string CounterName_WS001 = "WS001";
public const string CounterName_WS001_Help = "Description of the WS001";

public void Start()
{
      Console.WriteLine("UtilCounter Start ---------------------");

      if (!PerformanceCounterCategory.Exists(CounterCategoryName))
      {
            CounterCreationDataCollection counters = new CounterCreationDataCollection();
            counters.Add(new CounterCreationData(CounterName_WS001, CounterName_WS001_Help,                                  PerformanceCounterType.ElapsedTime));

            PerformanceCounterCategory.Create(
                     CounterCategoryName,
                     CounterCategoryNameHelp,
                     PerformanceCounterCategoryType.SingleInstance,counters);
       }

       // Test the Performance counters

       if(PerformanceCounterCategory.CounterExists(CounterName_WS001,CounterCategoryName))
       {
              PerformanceCounter pc = new PerformanceCounter(CounterCategoryName,                                  CounterName_WS001,false);

              pc.RawValue = 56;

              while(true)
              {
                       Thread.Sleep(3000);
                       pc.RawValue = 20;
                      Console.WriteLine("pc.RawValue ... ");
               }
        }
        Console.WriteLine("UtilCounter End --------------------");
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Tag Cloud

%d bloggers like this: