Any suggestions? Export (0) Print
Expand All

PerformanceCounter.RawValue Property


Gets or sets the raw, or uncalculated, value of this counter.

Namespace:   System.Diagnostics
Assembly:  System (in System.dll)

public long RawValue { get; set; }

Property Value

Type: System.Int64

The raw value of the counter.

Exception Condition

You are trying to set the counter's raw value, but the counter is read-only.


The instance is not correctly associated with a performance counter.


The InstanceLifetime property is set to Process when using global shared memory.


An error occurred when accessing a system API.


The platform is Windows 98 or Windows Millennium Edition (Me), which does not support performance counters.


Code that is executing without administrative privileges attempted to read a performance counter.

If the counter type is a 32-bit size and you attempt to set this property to a value that is too large to fit, the property truncates the value to 32 bits. When reading custom counters on the local computer, using the RawValue property rather than a calculated value can produce significantly better performance in scenarios where the raw value is sufficient.

If the counter that you are reading is read-only, getting the RawValue property samples the counter at the time that the property is called. This action is equivalent to making an initial call to the NextSample method. If you subsequently call NextSample, you can perform calculations on the values that both calls returned.

Because system counters are read-only, you can get but not set their raw values.


The Increment, IncrementBy, and Decrement methods use interlocks to update the counter value. This helps keep the counter value accurate in multithreaded or multiprocess scenarios, but also results in a performance penalty. If you do not need the accuracy that interlocked operations provide, you can update the RawValue property directly for up to a 5 times performance improvement. However, in multithreaded scenarios, some updates to the counter value might be ignored, resulting in inaccurate data.


If the value for the InstanceLifetime property is Process and the performance counter category was created with .NET Framework version 1.0 or 1.1, an InvalidOperationException is thrown. Performance counter categories created with earlier versions use global shared memory, and the value for InstanceLifetime must be Global. If the category is not used by applications running on versions 1.0 or 1.1 of the .NET Framework, delete and recreate the category.


To read performance counters in Windows Vista, Windows XP Professional x64 Edition, or Windows Server 2003, you must either be a member of the Performance Monitor Users group or have administrative privileges.

To avoid having to elevate your privileges to access performance counters in Windows Vista, add yourself to the Performance Monitor Users group.

In Windows Vista, User Account Control (UAC) determines the privileges of a user. If you are a member of the Built-in Administrators group, you are assigned two run-time access tokens: a standard user access token and an administrator access token. By default, you are in the standard user role. To execute the code that accesses performance counters, you must first elevate your privileges from standard user to administrator. You can do this when you start an application by right-clicking the application icon and indicating that you want to run as an administrator.

The following example uses the CounterSample class to display the value of the RawValue property for a counter.

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Diagnostics;

public class App {

	private static PerformanceCounter avgCounter64Sample;
	private static PerformanceCounter avgCounter64SampleBase;

	public static void Main()

		ArrayList samplesList = new ArrayList();

        // If the category does not exist, create the category and exit.
        // Performance counters should not be created and immediately used.
        // There is a latency time to enable the counters, they should be created
        // prior to executing the application that uses the counters.
        // Execute this sample a second time to use the category.
        if (SetupCategory())


	private static bool SetupCategory()
		if ( !PerformanceCounterCategory.Exists("AverageCounter64SampleCategory") ) 

			CounterCreationDataCollection counterDataCollection = new CounterCreationDataCollection();

			// Add the counter.
			CounterCreationData averageCount64 = new CounterCreationData();
			averageCount64.CounterType = PerformanceCounterType.AverageCount64;
			averageCount64.CounterName = "AverageCounter64Sample";

	        // Add the base counter.
			CounterCreationData averageCount64Base = new CounterCreationData();
			averageCount64Base.CounterType = PerformanceCounterType.AverageBase;
			averageCount64Base.CounterName = "AverageCounter64SampleBase";

			// Create the category.
                "Demonstrates usage of the AverageCounter64 performance counter type.",
                PerformanceCounterCategoryType.SingleInstance, counterDataCollection);

			Console.WriteLine("Category exists - AverageCounter64SampleCategory");

	private static void CreateCounters()
		// Create the counters.

		avgCounter64Sample = new PerformanceCounter("AverageCounter64SampleCategory", 

		avgCounter64SampleBase = new PerformanceCounter("AverageCounter64SampleCategory", 

	private static void CollectSamples(ArrayList samplesList)

		Random r = new Random( DateTime.Now.Millisecond );

		// Loop for the samples.
		for (int j = 0; j < 100; j++) 

			int value = r.Next(1, 10);
			Console.Write(j + " = " + value);



			if ((j % 10) == 9) 
				samplesList.Add( avgCounter64Sample.NextSample() );



	private static void CalculateResults(ArrayList samplesList)
		for(int i = 0; i < (samplesList.Count - 1); i++)
			// Output the sample.
			OutputSample( (CounterSample)samplesList[i] );
			OutputSample( (CounterSample)samplesList[i+1] );

			// Use .NET to calculate the counter value.
			Console.WriteLine(".NET computed counter value = " +
				(CounterSample)samplesList[i+1]) );

			// Calculate the counter value manually.
			Console.WriteLine("My computed counter value = " + 
				(CounterSample)samplesList[i+1]) );


	//	Description - This counter type shows how many items are processed, on average,
	//		during an operation. Counters of this type display a ratio of the items 
	//		processed (such as bytes sent) to the number of operations completed. The  
	//		ratio is calculated by comparing the number of items processed during the 
	//		last interval to the number of operations completed during the last interval. 
	// Generic type - Average
	//  	Formula - (N1 - N0) / (D1 - D0), where the numerator (N) represents the number 
	//		of items processed during the last sample interval and the denominator (D) 
	//		represents the number of operations completed during the last two sample 
	//		intervals. 
	//	Average (Nx - N0) / (Dx - D0)  
	//	Example PhysicalDisk\ Avg. Disk Bytes/Transfer 
	private static Single MyComputeCounterValue(CounterSample s0, CounterSample s1)
		Single numerator = (Single)s1.RawValue - (Single)s0.RawValue;
		Single denomenator = (Single)s1.BaseValue - (Single)s0.BaseValue;
		Single counterValue = numerator / denomenator;

	// Output information about the counter sample.
	private static void OutputSample(CounterSample s)
		Console.WriteLine("Sample values - \r\n");
		Console.WriteLine("   BaseValue        = " + s.BaseValue);
		Console.WriteLine("   CounterFrequency = " + s.CounterFrequency);
		Console.WriteLine("   CounterTimeStamp = " + s.CounterTimeStamp);
		Console.WriteLine("   CounterType      = " + s.CounterType);
		Console.WriteLine("   RawValue         = " + s.RawValue);
		Console.WriteLine("   SystemFrequency  = " + s.SystemFrequency);
		Console.WriteLine("   TimeStamp        = " + s.TimeStamp);
		Console.WriteLine("   TimeStamp100nSec = " + s.TimeStamp100nSec);


for reading the performance counter category. Associated enumeration: PerformanceCounterPermissionAccess.Read.


for writing the performance counter category. Associated enumeration: PerformanceCounterPermissionAccess.Write.

.NET Framework
Available since 1.1
Return to top
© 2016 Microsoft