title | ms.custom | ms.date | ms.reviewer | ms.suite | ms.technology | ms.tgt_pltfrm | ms.topic | dev_langs | helpviewer_keywords | ms.assetid | caps.latest.revision | author | ms.author | manager | |||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
How to: Read Windows Performance Counters (C++/CLI) | Microsoft Docs |
11/04/2016 |
|
article |
|
|
9e1c836c-cb0f-4f37-9a93-3dca6412d6b1 |
12 |
mikeblome |
mblome |
ghogen |
Some applications and Windows subsystems expose performance data through the Windows performance system. These counters can be accessed using the xref:System.Diagnostics.PerformanceCounterCategory and xref:System.Diagnostics.PerformanceCounter classes, which reside in the xref:System.Diagnostics?displayProperty=fullName namespace.
The following code example uses these classes to retrieve and display a counter that is updated by Windows to indicate the percentage of time that the processor is busy.
Note
This example requires administrative privileges to run on Windows Vista.
// processor_timer.cpp
// compile with: /clr
#using <system.dll>
using namespace System;
using namespace System::Threading;
using namespace System::Diagnostics;
using namespace System::Timers;
ref struct TimerObject
{
public:
static String^ m_instanceName;
static PerformanceCounter^ m_theCounter;
public:
static void OnTimer(Object^ source, ElapsedEventArgs^ e)
{
try
{
Console::WriteLine("CPU time used: {0,6} ",
m_theCounter->NextValue( ).ToString("f"));
}
catch(Exception^ e)
{
if (dynamic_cast<InvalidOperationException^>(e))
{
Console::WriteLine("Instance '{0}' does not exist",
m_instanceName);
return;
}
else
{
Console::WriteLine("Unknown exception... ('q' to quit)");
return;
}
}
}
};
int main()
{
String^ objectName = "Processor";
String^ counterName = "% Processor Time";
String^ instanceName = "_Total";
try
{
if ( !PerformanceCounterCategory::Exists(objectName) )
{
Console::WriteLine("Object {0} does not exist", objectName);
return -1;
}
}
catch (UnauthorizedAccessException ^ex)
{
Console::WriteLine("You are not authorized to access this information.");
Console::Write("If you are using Windows Vista, run the application with ");
Console::WriteLine("administrative privileges.");
Console::WriteLine(ex->Message);
return -1;
}
if ( !PerformanceCounterCategory::CounterExists(
counterName, objectName) )
{
Console::WriteLine("Counter {0} does not exist", counterName);
return -1;
}
TimerObject::m_instanceName = instanceName;
TimerObject::m_theCounter = gcnew PerformanceCounter(
objectName, counterName, instanceName);
System::Timers::Timer^ aTimer = gcnew System::Timers::Timer();
aTimer->Elapsed += gcnew ElapsedEventHandler(&TimerObject::OnTimer);
aTimer->Interval = 1000;
aTimer->Enabled = true;
aTimer->AutoReset = true;
Console::WriteLine("reporting CPU usage for the next 10 seconds");
Thread::Sleep(10000);
return 0;
}
Introduction to Monitoring Performance
Windows Operations (C++/CLI)
.NET Programming with C++/CLI (Visual C++)