1 Commits

Author SHA1 Message Date
f93983e992 Update to use Process V2 performance counter 2023-04-02 12:06:41 -04:00
6 changed files with 60 additions and 54 deletions

View File

@@ -1,30 +1,34 @@
<?xml version="1.0" encoding="utf-8" ?> <?xml version="1.0" encoding="utf-8"?>
<configuration> <configuration>
<configSections> <configSections>
<sectionGroup name="userSettings" type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" > <sectionGroup name="userSettings"
<section name="ProcessCpuUsageStatusWindow.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" /> type="System.Configuration.UserSettingsGroup, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089">
</sectionGroup> <section name="ProcessCpuUsageStatusWindow.Properties.Settings"
</configSections> type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089"
<startup> allowExeDefinition="MachineToLocalUser" requirePermission="false" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" /> </sectionGroup>
</startup> </configSections>
<userSettings> <startup>
<ProcessCpuUsageStatusWindow.Properties.Settings> <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8" />
<setting name="ProcessCount" serializeAs="String"> </startup>
<value>3</value> <userSettings>
</setting> <ProcessCpuUsageStatusWindow.Properties.Settings>
<setting name="UpdateInterval" serializeAs="String"> <setting name="ProcessCount" serializeAs="String">
<value>00:00:02</value> <value>3</value>
</setting> </setting>
<setting name="WindowSettings" serializeAs="String"> <setting name="UpdateInterval" serializeAs="String">
<value /> <value>00:00:02</value>
</setting> </setting>
<setting name="AutoStart" serializeAs="String"> <setting name="WindowSettings" serializeAs="String">
<value>True</value> <value />
</setting> </setting>
<setting name="FirstRun" serializeAs="String"> <setting name="AutoStart" serializeAs="String">
<value>True</value> <value>True</value>
</setting> </setting>
</ProcessCpuUsageStatusWindow.Properties.Settings> <setting name="FirstRun" serializeAs="String">
</userSettings> <value>True</value>
</setting>
</ProcessCpuUsageStatusWindow.Properties.Settings>
</userSettings>
</configuration> </configuration>

View File

@@ -40,18 +40,18 @@ namespace ProcessCpuUsageStatusWindow
CurrentProcessList = new Dictionary<string, ProcessCpuUsage>(); CurrentProcessList = new Dictionary<string, ProcessCpuUsage>();
// Get the category for process performance info // 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) if (_processCategory == null)
return; return;
// Read the entire category // Read the entire category
InstanceDataCollectionCollection processCategoryData = _processCategory.ReadCategory(); var processCategoryData = _processCategory.ReadCategory();
// Get the processor time data // 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; return;
// Loop over each instance and add it to the list // Loop over each instance and add it to the list
@@ -109,15 +109,15 @@ namespace ProcessCpuUsageStatusWindow
private void UpdateCurrentProcessList() private void UpdateCurrentProcessList()
{ {
// Get a timestamp for the current time that we can use to see if a process was found this check // 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 // Read the entire category
InstanceDataCollectionCollection processCategoryData = _processCategory.ReadCategory(); var processCategoryData = _processCategory.ReadCategory();
// Get the processor time data // 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; return;
// Loop over each instance and add it to the list // Loop over each instance and add it to the list
@@ -127,7 +127,7 @@ namespace ProcessCpuUsageStatusWindow
if (CurrentProcessList.ContainsKey(instanceData.InstanceName)) if (CurrentProcessList.ContainsKey(instanceData.InstanceName))
{ {
// Get the previous process usage object // Get the previous process usage object
ProcessCpuUsage processCpuUsage = CurrentProcessList[instanceData.InstanceName]; var processCpuUsage = CurrentProcessList[instanceData.InstanceName];
// Update the CPU usage with new data // Update the CPU usage with new data
processCpuUsage.UpdateCpuUsage(instanceData, checkStart); processCpuUsage.UpdateCpuUsage(instanceData, checkStart);
@@ -144,8 +144,8 @@ namespace ProcessCpuUsageStatusWindow
// Build a list of cached processes we haven't found this check // Build a list of cached processes we haven't found this check
var oldProcessList = (from processCpuUsage in CurrentProcessList var oldProcessList = (from processCpuUsage in CurrentProcessList
where processCpuUsage.Value.LastFound != checkStart where processCpuUsage.Value.LastFound != checkStart
select processCpuUsage.Key).ToList(); select processCpuUsage.Key).ToList();
// Loop over the list and remove the old process // Loop over the list and remove the old process
foreach (var key in oldProcessList) foreach (var key in oldProcessList)

View File

@@ -19,7 +19,7 @@ namespace ProcessCpuUsageStatusWindow.Properties {
// class via a tool like ResGen or Visual Studio. // class via a tool like ResGen or Visual Studio.
// To add or remove a member, edit your .ResX file then rerun ResGen // To add or remove a member, edit your .ResX file then rerun ResGen
// with the /str option, or rebuild your VS project. // 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.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()]
public class Resources { public class Resources {
@@ -225,7 +225,7 @@ namespace ProcessCpuUsageStatusWindow.Properties {
} }
/// <summary> /// <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> /// </summary>
public static string ProcessLine { public static string ProcessLine {
get { get {

View File

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

View File

@@ -12,7 +12,7 @@ namespace ProcessCpuUsageStatusWindow.Properties {
[global::System.Runtime.CompilerServices.CompilerGeneratedAttribute()] [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 { internal sealed partial class Settings : global::System.Configuration.ApplicationSettingsBase {
private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings()))); private static Settings defaultInstance = ((Settings)(global::System.Configuration.ApplicationSettingsBase.Synchronized(new Settings())));

View File

@@ -2,13 +2,10 @@
using FloatingStatusWindowLibrary; using FloatingStatusWindowLibrary;
using ProcessCpuUsageStatusWindow.Options; using ProcessCpuUsageStatusWindow.Options;
using ProcessCpuUsageStatusWindow.Properties; using ProcessCpuUsageStatusWindow.Properties;
using Squirrel;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Net;
using System.Text; using System.Text;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using System.Windows.Threading; using System.Windows.Threading;
@@ -117,7 +114,7 @@ namespace ProcessCpuUsageStatusWindow
private static class PredefinedProcessName private static class PredefinedProcessName
{ {
public const string Total = "_Total"; public const string Total = "_Total";
public const string Idle = "Idle"; public const string Idle = "Idle:0";
} }
private void UpdateDisplay(Dictionary<string, ProcessCpuUsage> currentProcessList) private void UpdateDisplay(Dictionary<string, ProcessCpuUsage> currentProcessList)
@@ -125,14 +122,14 @@ namespace ProcessCpuUsageStatusWindow
// Filter the process list to valid ones and exclude the idle and total values // Filter the process list to valid ones and exclude the idle and total values
var validProcessList = (currentProcessList.Values.Where( var validProcessList = (currentProcessList.Values.Where(
process => process =>
process.UsageValid && process.ProcessName != PredefinedProcessName.Total && process.UsageValid && process.ProcessName != PredefinedProcessName.Total &&
process.ProcessName != PredefinedProcessName.Idle)).ToList(); process.ProcessName != PredefinedProcessName.Idle)).ToList();
// Calculate the total usage by adding up all the processes we know about // Calculate the total usage by adding up all the processes we know about
var totalUsage = validProcessList.Sum(process => process.PercentUsage); var totalUsage = validProcessList.Sum(process => process.PercentUsage);
// Sort the process list by usage and take only the top few // 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 // Create a new string builder
var stringBuilder = new StringBuilder(); var stringBuilder = new StringBuilder();
@@ -146,14 +143,19 @@ namespace ProcessCpuUsageStatusWindow
} }
// Loop over all processes in the sorted list // 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 // Move to the next line if it isn't the first line
if (stringBuilder.Length != 0) if (stringBuilder.Length != 0)
stringBuilder.AppendLine(); 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 // 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) // Add the footer line (if any)