3 Commits

9 changed files with 150 additions and 62 deletions

View File

@@ -1,30 +1,37 @@
<?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>
<setting name="ShowProcessId" serializeAs="String">
<value>False</value>
</setting>
</ProcessCpuUsageStatusWindow.Properties.Settings>
</userSettings>
</configuration> </configuration>

View File

@@ -15,6 +15,7 @@
<ColumnDefinition Width="*" /> <ColumnDefinition Width="*" />
</Grid.ColumnDefinitions> </Grid.ColumnDefinitions>
<Grid.RowDefinitions> <Grid.RowDefinitions>
<RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="Auto"></RowDefinition> <RowDefinition Height="Auto"></RowDefinition>
<RowDefinition Height="*"></RowDefinition> <RowDefinition Height="*"></RowDefinition>
@@ -23,20 +24,34 @@
Name="StartWithWindows" Name="StartWithWindows"
VerticalAlignment="Top" VerticalAlignment="Top"
VerticalContentAlignment="Center" VerticalContentAlignment="Center"
Grid.Column="0"
Grid.Row="0"
Grid.ColumnSpan="2" /> Grid.ColumnSpan="2" />
<Label Grid.Column="0" <Label Grid.Column="0"
Grid.Row="1" Grid.Row="1"
Content="{x:Static properties:Resources.NumberOfProcesses}" Content="{x:Static properties:Resources.NumberOfProcesses}"
Margin="0,4,6,0" Margin="0,4,6,4"
Padding="0" Padding="0"
VerticalContentAlignment="Center" VerticalAlignment="Center" Target="{x:Reference NumberOfProcesses}" /> VerticalContentAlignment="Center"
VerticalAlignment="Center"
Target="{x:Reference NumberOfProcesses}" />
<xctk:IntegerUpDown Grid.Column="1" <xctk:IntegerUpDown Grid.Column="1"
Grid.Row="1" x:Name="NumberOfProcesses" Grid.Row="1"
Minimum="1" Maximum="20" x:Name="NumberOfProcesses"
TextAlignment="Left" Margin="0,12,6,6" Minimum="1"
Maximum="20"
TextAlignment="Left"
Margin="0,12,6,12"
Width="50" Width="50"
VerticalContentAlignment="Center" VerticalContentAlignment="Center"
VerticalAlignment="Center" VerticalAlignment="Center"
HorizontalAlignment="Left" /> HorizontalAlignment="Left" />
<CheckBox Content="{x:Static properties:Resources.ShowProcessId}"
Name="ShowProcessId"
VerticalAlignment="Top"
VerticalContentAlignment="Center"
Grid.Column="0"
Grid.Row="2"
Grid.ColumnSpan="2" />
</Grid> </Grid>
</windows:CategoryPanel> </windows:CategoryPanel>

View File

@@ -5,9 +5,13 @@ namespace ProcessCpuUsageStatusWindow.Options
{ {
public partial class GeneralOptionsPanel public partial class GeneralOptionsPanel
{ {
public GeneralOptionsPanel() private bool IsV2 { get; }
public GeneralOptionsPanel(bool isV2)
{ {
InitializeComponent(); InitializeComponent();
IsV2 = isV2;
} }
public override void LoadPanel(object data) public override void LoadPanel(object data)
@@ -18,6 +22,9 @@ namespace ProcessCpuUsageStatusWindow.Options
StartWithWindows.IsChecked = settings.AutoStart; StartWithWindows.IsChecked = settings.AutoStart;
NumberOfProcesses.Text = settings.ProcessCount.ToString(); NumberOfProcesses.Text = settings.ProcessCount.ToString();
ShowProcessId.IsChecked = settings.ShowProcessId;
ShowProcessId.Visibility = IsV2 ? Visibility.Visible : Visibility.Collapsed;
} }
public override bool ValidatePanel() public override bool ValidatePanel()
@@ -34,6 +41,9 @@ namespace ProcessCpuUsageStatusWindow.Options
settings.ProcessCount = int.Parse(NumberOfProcesses.Text); settings.ProcessCount = int.Parse(NumberOfProcesses.Text);
if (ShowProcessId.IsChecked.HasValue && settings.ShowProcessId != ShowProcessId.IsChecked.Value)
settings.ShowProcessId = ShowProcessId.IsChecked.Value;
Application.Current.SetStartWithWindows(settings.AutoStart); Application.Current.SetStartWithWindows(settings.AutoStart);
} }

View File

@@ -28,6 +28,8 @@ namespace ProcessCpuUsageStatusWindow
public Dictionary<string, ProcessCpuUsage> CurrentProcessList; public Dictionary<string, ProcessCpuUsage> CurrentProcessList;
public bool IsV2 => _processCategory.CategoryName == "Process V2";
#endregion #endregion
#region Initialize and terminate #region Initialize and terminate
@@ -40,18 +42,19 @@ 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") ??
PerformanceCounterCategory.GetCategories().FirstOrDefault(category => category.CategoryName == "Process");
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 +112,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 +130,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 +147,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 {
@@ -233,6 +233,15 @@ namespace ProcessCpuUsageStatusWindow.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to CPU: {1,4:f1}% - {0} ({2}).
/// </summary>
public static string ProcessLineWithProcessId {
get {
return ResourceManager.GetString("ProcessLineWithProcessId", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to Restarting application.... /// Looks up a localized string similar to Restarting application....
/// </summary> /// </summary>
@@ -242,6 +251,15 @@ namespace ProcessCpuUsageStatusWindow.Properties {
} }
} }
/// <summary>
/// Looks up a localized string similar to Show _process ID.
/// </summary>
public static string ShowProcessId {
get {
return ResourceManager.GetString("ShowProcessId", resourceCulture);
}
}
/// <summary> /// <summary>
/// Looks up a localized string similar to _Start when Windows starts. /// Looks up a localized string similar to _Start when Windows starts.
/// </summary> /// </summary>

View File

@@ -184,4 +184,10 @@
<data name="RestartingAfterUpdate" xml:space="preserve"> <data name="RestartingAfterUpdate" xml:space="preserve">
<value>Restarting application...</value> <value>Restarting application...</value>
</data> </data>
<data name="ProcessLineWithProcessId" xml:space="preserve">
<value>CPU: {1,4:f1}% - {0} ({2})</value>
</data>
<data name="ShowProcessId" xml:space="preserve">
<value>Show _process ID</value>
</data>
</root> </root>

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())));
@@ -82,5 +82,17 @@ namespace ProcessCpuUsageStatusWindow.Properties {
this["FirstRun"] = value; this["FirstRun"] = value;
} }
} }
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("False")]
public bool ShowProcessId {
get {
return ((bool)(this["ShowProcessId"]));
}
set {
this["ShowProcessId"] = value;
}
}
} }
} }

View File

@@ -17,5 +17,8 @@
<Setting Name="FirstRun" Type="System.Boolean" Scope="User"> <Setting Name="FirstRun" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value> <Value Profile="(Default)">True</Value>
</Setting> </Setting>
<Setting Name="ShowProcessId" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
</Settings> </Settings>
</SettingsFile> </SettingsFile>

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;
@@ -65,7 +62,7 @@ namespace ProcessCpuUsageStatusWindow
{ {
var panels = new List<CategoryPanel> var panels = new List<CategoryPanel>
{ {
new GeneralOptionsPanel(), new GeneralOptionsPanel(_processCpuUsageWatcher.IsV2),
new AboutOptionsPanel() new AboutOptionsPanel()
}; };
@@ -118,6 +115,7 @@ namespace ProcessCpuUsageStatusWindow
{ {
public const string Total = "_Total"; public const string Total = "_Total";
public const string Idle = "Idle"; public const string Idle = "Idle";
public const string IdleWithProcessId = "Idle:0";
} }
private void UpdateDisplay(Dictionary<string, ProcessCpuUsage> currentProcessList) private void UpdateDisplay(Dictionary<string, ProcessCpuUsage> currentProcessList)
@@ -125,14 +123,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 != (_processCpuUsageWatcher.IsV2 ? PredefinedProcessName.IdleWithProcessId : 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 +144,30 @@ 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();
// Format the process information into a string to display if (_processCpuUsageWatcher.IsV2)
stringBuilder.AppendFormat(Resources.ProcessLine, processCpuUsage.ProcessName, processCpuUsage.PercentUsage); {
// Split the process name from the process ID
var colonPosition = processCpuUsage.ProcessName.LastIndexOf(':');
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(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) // Add the footer line (if any)