The performance counter is in the Performance Counter type. Therefore, to create a performance counter, you must first create a Performance Counter type. However, the two steps can be completed through a function call.
Note:
To create a performance counter, you must run the program as an administrator. Otherwise, a system. unauthorizedaccessexception exception is thrown.
To create a performance counter, you must call performancecountercategory. Create. The parameter is type name, type description, type of the Performance Counter type (instance type), and the data of the counter is created.
The data used to create a counter is countercreationdatacollection. This class stores a series of countercreationdata, which specifies the name and type of the performance counter (there are other unimportant information ).
After creating a performance counter, you can write the value to it. The step is to create a performance counter object that is not read-only: performancecounter type, set readonly to false (which can be set in the constructor ). Then, you can use the performancecounter's increase, decrease, increaseby, and decreaseby methods to change the original value of the performance counter. These operations are atomic. Of course, if you do not need thread atomicity to update the performance counter, you can directly modify performancecounter. rawvalue to change the original value of the performance counter, which can save some performance.
The following code creates a custom performance counter, reads the value while writing the value, and ends the output by pressing any key. (Note: The first operation will first create a performance counter, so the program needs to be run as an administrator. However, the subsequent operation does not require the Administrator identity, because it is only the original value of the write and read performance counters)
Using system;
Using system. Collections. Generic;
Using system. LINQ;
Using system. text;
Using system. reflection;
Using system. diagnostics;
Using system. Threading;
Namespace mgen
{
Class Program
{
Static void main ()
{
// Try to create the Performance Counter type and Performance Counter
Create ();
// Read the original value of the performance counter
Threadpool. queueuserworkitem (ARG) => readvalues ());
// Original Value of the Write Performance Counter
Threadpool. queueuserworkitem (ARG) => writesvalues ());
// Console Key Control
Console. readkey (true );
}
Static void create ()
{
// Determine whether the object already exists
If (performancecountercategory. exists ("My type "))
Return;
// Create countercreationdata
VaR CCD = new countercreationdata ();
// Name: My counter; Type: numberofitems64
CCD. countername = "My counters ";
CCD. countertype = performancecountertype. numberofitems64;
Performancecountercategory. Create ("My type", // type name
"Mgen counter test", // type description
Performancecountercategorytype. singleinstance, // type of instance type
New countercreationdatacollection () {CCD}); // create performance counter data
}
Static void writesvalues ()
{
// Check whether the type or counter does not exist
If (! Performancecountercategory. exists ("My type") |! Performancecountercategory. counterexists ("mycounter", "mycategory "))
{
Console. writeline ("the expected performance counter does not exist ");
Return;
}
// Add the original counter value
Using (VAR couter = new performancecounter ("My type", "My counters", false ))
{
While (true)
{
Couter. incrementby (2 );
Thread. Sleep (1000 );
}
}
}
Static void readvalues ()
{
// Check whether the type or counter does not exist
If (! Performancecountercategory. exists ("My type") |! Performancecountercategory. counterexists ("mycounter", "mycategory "))
{
Console. writeline ("the expected performance counter does not exist ");
Return;
}
// Read the original counter value
Using (VAR couter = new performancecounter ("My type", "My counters", true ))
{
While (true)
{
Console. writeline (couter. rawvalue );
Thread. Sleep (1000 );
}
}
}
}
}
The program output will gradually add the original value of the performance counter to 2:
2
4
6
8
...
You can also read the value of this performance counter in Windows performance monitor.
Open "performance counters" in the control panel, add performance counters, and select "My counters" in our custom "My types:
Then change the type to "report" to read the original value of the performance counter:
The result is as follows: (display of Performance Monitor on the left and output of our program on the right)