BUG: NextValue method of .NET PerformanceCounter object returns zero (324548)



The information in this article applies to:

  • Microsoft .NET Framework 1.0
  • Microsoft .NET Framework 1.1
  • Microsoft .NET Framework Class Libraries 1.0
  • Microsoft .NET Framework Class Libraries 1.1

This article was previously published under Q324548

SYMPTOMS

The NextValue method of the .NET PerformanceCounter object may return zero (0) for the following LogicalDisk or PhysicalDisk performance counters:
  • % Disk Time
  • % Disk Read Time
  • % Disk Write Time
  • % Idle Time
  • Avg. Disk Queue Length
  • Avg. Disk Read Queue Length
  • Avg. Disk Write Queue Length

CAUSE

The performance counters that are listed in the "Symptoms" section use the following counter types:
  • PERF_PRECISION_100NS_TIMER
  • PERF_PRECISION_TIMESTAMP
  • PERF_COUNTER_100NS_QUEUELEN_TYPE
This problem occurs because the implementation of the .NET PerformanceCounter class does not correctly handle these counter types.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the "Applies to" section.

MORE INFORMATION

The following C# sample code reproduces the problem:
using System;
using System.Diagnostics;
using System.Threading;

public class function 
{
	public static void Main() 
	{
		float value;
		int i;
		PerformanceCounter objPerfMon;
		string machineName = ".";
		string objectName = "PhysicalDisk";
        
		try 
		{
			for (i=0; i<10; i++) 
			{ 
				objPerfMon = new PerformanceCounter(objectName, "% Disk Time", "_Total", machineName);
				value = objPerfMon.NextValue();
				Thread.Sleep(100);
				value = objPerfMon.NextValue();
				Console.WriteLine("% Disk Time " + value.ToString());
			}
			for (i=0; i<10; i++) 
			{ 
				objPerfMon = new PerformanceCounter(objectName, "% Disk Read Time", "_Total", machineName);
				value = objPerfMon.NextValue();
				Thread.Sleep(100);
				value = objPerfMon.NextValue();
				Console.WriteLine("% Disk Read Time " + value.ToString());
			}
			for (i=0; i<10; i++) 
			{ 
				objPerfMon = new PerformanceCounter(objectName, "% Disk Write Time", "_Total", machineName);
				value = objPerfMon.NextValue();
				Thread.Sleep(100);
				value = objPerfMon.NextValue();
				Console.WriteLine("% Disk Write Time " + value.ToString());
			}
			for (i=0; i<10; i++) 
			{ 
				objPerfMon = new PerformanceCounter(objectName, "% Idle Time", "_Total", machineName);
				value = objPerfMon.NextValue();
				Thread.Sleep(100);
				value = objPerfMon.NextValue();
				Console.WriteLine("% Idle Time " + value.ToString());
			}

			for (i=0; i<10; i++) 
			{ 
				objPerfMon = new PerformanceCounter(objectName, "Avg. Disk Queue Length", "_Total", machineName);
				value = objPerfMon.NextValue();
				Thread.Sleep(100);
				value = objPerfMon.NextValue();
				Console.WriteLine("Avg. Disk Queue Length " + value.ToString());
			}

			for (i=0; i<10; i++) 
			{ 
				objPerfMon = new PerformanceCounter(objectName, "Avg. Disk Read Queue Length", "_Total", machineName);
				value = objPerfMon.NextValue();
				Thread.Sleep(100);
				value = objPerfMon.NextValue();
				Console.WriteLine("Avg. Disk Read Queue Length " + value.ToString());
			}

			for (i=0; i<10; i++) 
			{ 
				objPerfMon = new PerformanceCounter(objectName, "Avg. Disk Write Queue Length", "_Total", machineName);
				value = objPerfMon.NextValue();
				Thread.Sleep(100);
				value = objPerfMon.NextValue();
				Console.WriteLine("Avg. Disk Write Queue Length " + value.ToString());
			}
		}
		catch (InvalidOperationException) 
		{
			Console.WriteLine("N/A");
		}
		catch (Exception ex) 
		{
			Console.WriteLine(ex.ToString());
		}
	}
}
	

REFERENCES

For additional information, click the following article numbers to view the articles in the Microsoft Knowledge Base:

269481 Changes to LogicalDisk and PhysicalDisk precision counter types for Windows

262937 RegQueryValueEx() may not return disk performance counters


Modification Type:MajorLast Reviewed:4/30/2004
Keywords:kbBCL kbbug kbDiagnostics kbKernBase kbnofix kbPerfMon KB324548 kbAudDeveloper