Skip to content

Commit 7818414

Browse files
committed
perf: Improved performance counter initialisation
1 parent ab4f4bd commit 7818414

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

Source/RunActivity/Viewer3D/Popups/HUDWindow.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// COPYRIGHT 2011, 2012, 2013 by the Open Rails project.
1+
// COPYRIGHT 2011, 2012, 2013 by the Open Rails project.
22
//
33
// This file is part of Open Rails.
44
//
@@ -112,14 +112,15 @@ public HUDWindow(WindowManager owner)
112112
ProcessMemoryCounters = new PROCESS_MEMORY_COUNTERS() { Size = 40 };
113113
ProcessVirtualAddressLimit = GetVirtualAddressLimit();
114114

115+
var processId = Process.GetCurrentProcess().Id;
115116
try
116117
{
117118
var counterDotNetClrMemory = new PerformanceCounterCategory(".NET CLR Memory");
118-
foreach (var process in counterDotNetClrMemory.GetInstanceNames())
119+
foreach (InstanceData instance in counterDotNetClrMemory.ReadCategory()["Process ID"].Values)
119120
{
120-
var processId = new PerformanceCounter(".NET CLR Memory", "Process ID", process);
121-
if (processId.NextValue() == Process.GetCurrentProcess().Id)
121+
if (instance.RawValue == processId)
122122
{
123+
var process = instance.InstanceName;
123124
CLRMemoryAllocatedBytesPerSecCounter = new PerformanceCounter(".NET CLR Memory", "Allocated Bytes/sec", process);
124125
Trace.TraceInformation($"Found Microsoft .NET Framework performance counter {process}");
125126
break;
@@ -135,11 +136,11 @@ public HUDWindow(WindowManager owner)
135136
try
136137
{
137138
var counterProcess = new PerformanceCounterCategory("Process");
138-
foreach (var process in counterProcess.GetInstanceNames())
139+
foreach (InstanceData instance in counterProcess.ReadCategory()["ID Process"].Values)
139140
{
140-
var processId = new PerformanceCounter("Process", "ID Process", process);
141-
if (processId.NextValue() == Process.GetCurrentProcess().Id)
141+
if (instance.RawValue == processId)
142142
{
143+
var process = instance.InstanceName;
143144
CPUMemoryPrivateCounter = new PerformanceCounter("Process", "Private Bytes", process);
144145
CPUMemoryWorkingSetCounter = new PerformanceCounter("Process", "Working Set", process);
145146
CPUMemoryWorkingSetPrivateCounter = new PerformanceCounter("Process", "Working Set - Private", process);

0 commit comments

Comments
 (0)