I'm trying to use the "% Disk Time" Preformance Counter under the Physical
Disk category but it always returns a value of zero. I noticed the
CounterType is blank in the Properties window so not sure if this means
anything. Is this counter available in XP? If so, do I need to do some
additional work to get a valid value?
I'm running XP and developing on C# .NET and PerfMon.exe reflects changing
values for "% Disk Time".
Any insight is appreciated.
Basil
private System.Diagnostics.PerformanceCounter DiskIOPercent;
public MyDiskIOTestApp()
{
InitializeComponent();
// initialize the diskIO counter
float DiskIO = DiskIOPercent.NextValue();
while (true)
{
DiskIO = DiskIOPercent.NextValue();
if (DiskIO > 0.0)
break;
// sleep for 10 seconds
System.Threading.Thread.Sleep( new TimeSpan( 0, 0, 10 ) );
}
}
private void InitializeComponent()
{
this.DiskIOPercent = new System.Diagnostics.PerformanceCounter();
((System.ComponentModel.ISupportInitialize)(this.DiskIOPercent )).BeginInit(
);
//
// DiskIOPercent
//
this.DiskIOPercent.CategoryName = "PhysicalDisk";
this.DiskIOPercent.CounterName = "% Disk Time";
this.DiskIOPercent.InstanceName = "_Total";
((System.ComponentModel.ISupportInitialize)(this.DiskIOPercent )).EndInit();
}
"Willy Denoyette [MVP]" <willy.d...@pandora.be> wrote in message
news:#sQ$2MoiCHA.1784@tkmsftngp09...
> Please post your code.
>
> Willy.
>
> "BMeyer" <bme...@krollontrack.com> wrote in message
news:#7NxainiCHA.2580@tkmsftngp12...
Willy.
"BMeyer" <bme...@krollontrack.com> wrote in message news:#7NxainiCHA.2580@tkmsftngp12...
BUG: NextValue Method of .NET PerformanceCounter Object Returns Zero
http://support.microsoft.com/default.aspx?scid=kb;en-us;Q324548
Thanks,
Li-Yan Zhang
VS.NET, Visual C++
Microsoft
This posting is provided "AS IS" with no warranties, and confers no rights.
Got .Net? http://www.gotdotnet.com
"Li-Yan Zhang [MS]" <ly...@online.microsoft.com> wrote in message
news:QR#mcVviCHA.2604@cpmsftngxa06...
using System;
using System.Diagnostics;
using System.Threading;
using System.Timers;
using System.Management;
class Diskperf
{
ManagementObject pdisk;
decimal drpct;
decimal drpctm;
ulong prevCountBase = 0;
ulong newCountBase = 0;
ulong prevCountTime = 0;
ulong newCountTime = 0;
string counter = null;
internal decimal DiskReadPct {
get
{
return drpct;
}
}
public int Counter{
set
{
switch (value) {
case 1:
counter = "PercentDiskTime";
break;
case 2:
counter = "PercentDiskReadTime";
break;
case 3:
counter = "PercentDiskWriteTime";
break;
}
}
}
internal decimal DiskReadPctMax{
get
{
return drpctm;
}
}
public Diskperf(string instance)
{
// Using Raw Physical disk performance counters
pdisk = new ManagementObject("Win32_PerfRawData_PerfDisk_PhysicalDisk.Name='" + instance + "'");
Console.Write("Current Highest\n----------------- ");
}
public void OnTimer(Object source, ElapsedEventArgs e)
{
pdisk.Get();
newCountBase = (ulong)pdisk.Properties[counter + "_Base"].Value;
newCountTime = (ulong)pdisk.Properties[counter].Value;
decimal pdbde = Convert.ToDecimal(prevCountBase);
decimal pdtde = Convert.ToDecimal(prevCountTime);
decimal pdbd = Convert.ToDecimal(newCountBase);
decimal pdtd = Convert.ToDecimal(newCountTime);
drpct = (pdtde - pdtd) *100m / ( pdbde - pdbd) ;
// save highest value
if (drpct > drpctm)
drpctm = drpct;
Console.WriteLine("{0:f2} \t {1:f2}",DiskReadPct, DiskReadPctMax);
prevCountBase = newCountBase;
prevCountTime = newCountTime;
System.Timers.Timer theTimer = (System.Timers.Timer)source;
theTimer.Enabled = true;
}
public void Close()
{
pdisk.Dispose();
}
}
public class PCReadWMI {
static readonly int disktime = 1;
public static void Main(string[] args){
string instance = "_total";
string appName = Environment.GetCommandLineArgs()[0];
int _counter = 0;
if(args.Length > 1)
instance = args[1];
// instance = _total (default) or any valid value like "0 C:"
if (args.Length > 0) {
if(Convert.ToInt32(args[0]) > 3) {
Console.WriteLine("Usage: {0} <counter> (values : 1 = % Disk time; 2 = % Read Time; 3 = % WriteTime) [<instance>]", appName);
return;
}
_counter = Convert.ToInt32(args[0]);
}
else
_counter = disktime;
Diskperf dp = new Diskperf(instance);
dp.Counter = _counter;
System.Timers.Timer aTimer = new System.Timers.Timer();
aTimer.Elapsed += new ElapsedEventHandler(dp.OnTimer);
aTimer.Interval = 1000;
aTimer.Enabled = true;
aTimer.AutoReset = false;
Console.WriteLine("Press \'q\' to quit the sample");
while ( Console.Read()!='q' ) {
Thread.Sleep(1000);
}
dp.Close();
}
}
Willy.
"BMeyer" <bme...@krollontrack.com> wrote in message news:OkC#wnziCHA.1884@tkmsftngp10...