Update to use Process V2 performance counter

This commit is contained in:
2023-04-02 12:06:41 -04:00
parent d9d0f33788
commit f93983e992
6 changed files with 60 additions and 54 deletions

View File

@@ -1,12 +1,16 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" >
<section name="ProcessCpuUsageStatusWindow.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<sectionGroup name="userSettings"
type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
<section name="ProcessCpuUsageStatusWindow.Properties.Settings"
type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
</startup>
<userSettings>
<ProcessCpuUsageStatusWindow.Properties.Settings>

View File

@@ -40,18 +40,18 @@ namespace ProcessCpuUsageStatusWindow
CurrentProcessList = new Dictionary<string, ProcessCpuUsage>();
// Get the category for process performance info
_processCategory = PerformanceCounterCategory.GetCategories().FirstOrDefault(category => category.CategoryName == "Process");
_processCategory = PerformanceCounterCategory.GetCategories().FirstOrDefault(category => category.CategoryName == "Process V2");
if (_processCategory == null)
return;
// Read the entire category
InstanceDataCollectionCollection processCategoryData = _processCategory.ReadCategory();
var processCategoryData = _processCategory.ReadCategory();
// Get the processor time data
InstanceDataCollection processorTimeData = processCategoryData["% processor time"];
var processorTimeData = processCategoryData["% processor time"];
if (processorTimeData == null || processorTimeData.Values == null)
if (processorTimeData?.Values == null)
return;
// Loop over each instance and add it to the list
@@ -109,15 +109,15 @@ namespace ProcessCpuUsageStatusWindow
private void UpdateCurrentProcessList()
{
// Get a timestamp for the current time that we can use to see if a process was found this check
DateTime checkStart = DateTime.Now;
var checkStart = DateTime.Now;
// Read the entire category
InstanceDataCollectionCollection processCategoryData = _processCategory.ReadCategory();
var processCategoryData = _processCategory.ReadCategory();
// Get the processor time data
InstanceDataCollection processorTimeData = processCategoryData["% processor time"];
var processorTimeData = processCategoryData["% processor time"];
if (processorTimeData == null || processorTimeData.Values == null)
if (processorTimeData?.Values == null)
return;
// Loop over each instance and add it to the list
@@ -127,7 +127,7 @@ namespace ProcessCpuUsageStatusWindow
if (CurrentProcessList.ContainsKey(instanceData.InstanceName))
{
// Get the previous process usage object
ProcessCpuUsage processCpuUsage = CurrentProcessList[instanceData.InstanceName];
var processCpuUsage = CurrentProcessList[instanceData.InstanceName];
// Update the CPU usage with new data
processCpuUsage.UpdateCpuUsage(instanceData, checkStart);

View File

@@ -19,7 +19,7 @@ namespace ProcessCpuUsageStatusWindow.Properties {
// class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project.
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "15.0.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("System.Resources.Tools.StronglyTypedResourceBuilder", "17.0.0.0")]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class Resources {
@@ -225,7 +225,7 @@ namespace ProcessCpuUsageStatusWindow.Properties {
}
/// <summary>
/// Looks up a localized string similar to CPU: {1,4:f1}% - {0}.
/// Looks up a localized string similar to CPU: {1,4:f1}% - {0} ({2}).
/// </summary>
public static string ProcessLine {
get {

View File

@@ -173,7 +173,7 @@
<value>Options</value>
</data>
<data name="ProcessLine" xml:space="preserve">
<value>CPU: {1,4:f1}% - {0}</value>
<value>CPU: {1,4:f1}% - {0} ({2})</value>
</data>
<data name="StartWithWindows" xml:space="preserve">
<value>_Start when Windows starts</value>

View File

@@ -12,7 +12,7 @@ namespace ProcessCpuUsageStatusWindow.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "15.5.0.0")]
[global::System.CodeDom.Compiler.GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "17.5.0.0")]
internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

View File

@@ -2,13 +2,10 @@
using FloatingStatusWindowLibrary;
using ProcessCpuUsageStatusWindow.Options;
using ProcessCpuUsageStatusWindow.Properties;
using Squirrel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Threading;
@@ -117,7 +114,7 @@ namespace ProcessCpuUsageStatusWindow
private static class PredefinedProcessName
{
public const string Total = "_Total";
public const string Idle = "Idle";
public const string Idle = "Idle:0";
}
private void UpdateDisplay(Dictionary<string, ProcessCpuUsage> currentProcessList)
@@ -132,7 +129,7 @@ namespace ProcessCpuUsageStatusWindow
var totalUsage = validProcessList.Sum(process => process.PercentUsage);
// Sort the process list by usage and take only the top few
var sortedProcessList = (validProcessList.OrderByDescending(process => process.PercentUsage)).Take(Settings.Default.ProcessCount);
var sortedProcessList = validProcessList.OrderByDescending(process => process.PercentUsage).Take(Settings.Default.ProcessCount);
// Create a new string builder
var stringBuilder = new StringBuilder();
@@ -146,14 +143,19 @@ namespace ProcessCpuUsageStatusWindow
}
// Loop over all processes in the sorted list
foreach (ProcessCpuUsage processCpuUsage in sortedProcessList)
foreach (var processCpuUsage in sortedProcessList)
{
// Move to the next line if it isn't the first line
if (stringBuilder.Length != 0)
stringBuilder.AppendLine();
var colonPosition = processCpuUsage.ProcessName.LastIndexOf(':');
var processName = colonPosition == -1 ? processCpuUsage.ProcessName : processCpuUsage.ProcessName.Substring(0, colonPosition);
var processId = colonPosition == -1 ? string.Empty : processCpuUsage.ProcessName.Substring(colonPosition + 1);
// Format the process information into a string to display
stringBuilder.AppendFormat(Resources.ProcessLine, processCpuUsage.ProcessName, processCpuUsage.PercentUsage);
stringBuilder.AppendFormat(Resources.ProcessLine, processName, processCpuUsage.PercentUsage, processId);
}
// Add the footer line (if any)