From eebd23f702023325530293d2b24678fef883c382 Mon Sep 17 00:00:00 2001 From: Chris Kaczor Date: Mon, 3 Apr 2023 08:23:20 -0400 Subject: [PATCH] Handle when Process V2 isn't available --- Options/GeneralOptionsPanel.xaml.cs | 8 +++++++- ProcessCpuUsageWatcher.cs | 5 ++++- WindowSource.cs | 28 ++++++++++++++++++++-------- 3 files changed, 31 insertions(+), 10 deletions(-) diff --git a/Options/GeneralOptionsPanel.xaml.cs b/Options/GeneralOptionsPanel.xaml.cs index c013829..04c8f0e 100644 --- a/Options/GeneralOptionsPanel.xaml.cs +++ b/Options/GeneralOptionsPanel.xaml.cs @@ -5,9 +5,13 @@ namespace ProcessCpuUsageStatusWindow.Options { public partial class GeneralOptionsPanel { - public GeneralOptionsPanel() + private bool IsV2 { get; } + + public GeneralOptionsPanel(bool isV2) { InitializeComponent(); + + IsV2 = isV2; } public override void LoadPanel(object data) @@ -19,6 +23,8 @@ namespace ProcessCpuUsageStatusWindow.Options StartWithWindows.IsChecked = settings.AutoStart; NumberOfProcesses.Text = settings.ProcessCount.ToString(); ShowProcessId.IsChecked = settings.ShowProcessId; + + ShowProcessId.Visibility = IsV2 ? Visibility.Visible : Visibility.Collapsed; } public override bool ValidatePanel() diff --git a/ProcessCpuUsageWatcher.cs b/ProcessCpuUsageWatcher.cs index cc0c4dc..e4e61ed 100644 --- a/ProcessCpuUsageWatcher.cs +++ b/ProcessCpuUsageWatcher.cs @@ -28,6 +28,8 @@ namespace ProcessCpuUsageStatusWindow public Dictionary CurrentProcessList; + public bool IsV2 => _processCategory.CategoryName == "Process V2"; + #endregion #region Initialize and terminate @@ -40,7 +42,8 @@ namespace ProcessCpuUsageStatusWindow CurrentProcessList = new Dictionary(); // Get the category for process performance info - _processCategory = PerformanceCounterCategory.GetCategories().FirstOrDefault(category => category.CategoryName == "Process V2"); + _processCategory = PerformanceCounterCategory.GetCategories().FirstOrDefault(category => category.CategoryName == "Process V2") ?? + PerformanceCounterCategory.GetCategories().FirstOrDefault(category => category.CategoryName == "Process"); if (_processCategory == null) return; diff --git a/WindowSource.cs b/WindowSource.cs index da17be7..6f8b5d7 100644 --- a/WindowSource.cs +++ b/WindowSource.cs @@ -62,7 +62,7 @@ namespace ProcessCpuUsageStatusWindow { var panels = new List { - new GeneralOptionsPanel(), + new GeneralOptionsPanel(_processCpuUsageWatcher.IsV2), new AboutOptionsPanel() }; @@ -114,7 +114,8 @@ namespace ProcessCpuUsageStatusWindow private static class PredefinedProcessName { public const string Total = "_Total"; - public const string Idle = "Idle:0"; + public const string Idle = "Idle"; + public const string IdleWithProcessId = "Idle:0"; } private void UpdateDisplay(Dictionary currentProcessList) @@ -123,7 +124,7 @@ namespace ProcessCpuUsageStatusWindow var validProcessList = (currentProcessList.Values.Where( process => process.UsageValid && process.ProcessName != PredefinedProcessName.Total && - process.ProcessName != PredefinedProcessName.Idle)).ToList(); + process.ProcessName != (_processCpuUsageWatcher.IsV2 ? PredefinedProcessName.IdleWithProcessId : PredefinedProcessName.Idle))).ToList(); // Calculate the total usage by adding up all the processes we know about var totalUsage = validProcessList.Sum(process => process.PercentUsage); @@ -149,13 +150,24 @@ namespace ProcessCpuUsageStatusWindow if (stringBuilder.Length != 0) stringBuilder.AppendLine(); - var colonPosition = processCpuUsage.ProcessName.LastIndexOf(':'); + if (_processCpuUsageWatcher.IsV2) + { + // Split the process name from the process ID + 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); + var processName = processCpuUsage.ProcessName.Substring(0, colonPosition); + var processId = processCpuUsage.ProcessName.Substring(colonPosition + 1); - // Format the process information into a string to display - stringBuilder.AppendFormat(Settings.Default.ShowProcessId ? Resources.ProcessLineWithProcessId : Resources.ProcessLine, processName, processCpuUsage.PercentUsage, processId); + var formatString = Settings.Default.ShowProcessId ? Resources.ProcessLineWithProcessId : Resources.ProcessLine; + + // Format the process information into a string to display + stringBuilder.AppendFormat(formatString, processName, processCpuUsage.PercentUsage, processId); + } + else + { + // Format the process information into a string to display + stringBuilder.AppendFormat(Resources.ProcessLine, processCpuUsage.ProcessName, processCpuUsage.PercentUsage); + } } // Add the footer line (if any)