Handle when Process V2 isn't available

This commit is contained in:
2023-04-03 08:23:20 -04:00
parent ebd9dc607b
commit eebd23f702
3 changed files with 31 additions and 10 deletions

View File

@@ -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()

View File

@@ -28,6 +28,8 @@ namespace ProcessCpuUsageStatusWindow
public Dictionary<string, ProcessCpuUsage> CurrentProcessList;
public bool IsV2 => _processCategory.CategoryName == "Process V2";
#endregion
#region Initialize and terminate
@@ -40,7 +42,8 @@ namespace ProcessCpuUsageStatusWindow
CurrentProcessList = new Dictionary<string, ProcessCpuUsage>();
// 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;

View File

@@ -62,7 +62,7 @@ namespace ProcessCpuUsageStatusWindow
{
var panels = new List<CategoryPanel>
{
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<string, ProcessCpuUsage> 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();
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);
var formatString = Settings.Default.ShowProcessId ? Resources.ProcessLineWithProcessId : Resources.ProcessLine;
// Format the process information into a string to display
stringBuilder.AppendFormat(Settings.Default.ShowProcessId ? Resources.ProcessLineWithProcessId : Resources.ProcessLine, processName, processCpuUsage.PercentUsage, processId);
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)