Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added a feature that users can adjust CPU timer interval #101

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions RunCat/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
<?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="RunCat.Properties.UserSettings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<userSettings>
<RunCat.Properties.UserSettings>
<setting name="Runner" serializeAs="String">
<value>cat</value>
</setting>
<setting name="Theme" serializeAs="String">
<value />
</setting>
<setting name="Speed" serializeAs="String">
<value>default</value>
</setting>
<setting name="CpuTimerInterval" serializeAs="String">
<value>3000</value>
</setting>
</RunCat.Properties.UserSettings>
</userSettings>
</configuration>
88 changes: 56 additions & 32 deletions RunCat/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
using System.Windows.Forms;
using System.Resources;
using System.ComponentModel;
using System.Threading.Tasks;

namespace RunCat
{
Expand All @@ -47,23 +48,23 @@ static void Main()

public class RunCatApplicationContext : ApplicationContext
{
private const int CPU_TIMER_DEFAULT_INTERVAL = 3000;
private const int ANIMATE_TIMER_DEFAULT_INTERVAL = 200;
private PerformanceCounter cpuUsage;
private ToolStripMenuItem runnerMenu;
private ToolStripMenuItem themeMenu;
private ToolStripMenuItem startupMenu;
private ToolStripMenuItem runnerSpeedLimit;
private ToolStripMenuItem cpuTimerIntervalMenu;
private NotifyIcon notifyIcon;
private string runner = "";
private int cpuTimerInterval = UserSettings.Default.CpuTimerInterval;
private int current = 0;
private float minCPU;
private float interval;
private string systemTheme = "";
private string manualTheme = UserSettings.Default.Theme;
private string speed = UserSettings.Default.Speed;
private Icon[] icons;
private Timer animateTimer = new Timer();
private Timer cpuTimer = new Timer();


Expand Down Expand Up @@ -142,13 +143,43 @@ public RunCatApplicationContext()
}
});

cpuTimerIntervalMenu = new ToolStripMenuItem("CPU Update Interval", null, new ToolStripMenuItem[]
{
new ToolStripMenuItem("3 Seconds", null, SetCpuTimerInterval)
{
Checked = cpuTimerInterval.Equals(3000),
Tag = 3000
},
new ToolStripMenuItem("1 Second", null, SetCpuTimerInterval)
{
Checked = cpuTimerInterval.Equals(1000),
Tag = 1000
},
new ToolStripMenuItem("0.5 Second", null, SetCpuTimerInterval)
{
Checked = cpuTimerInterval.Equals(500),
Tag = 500
},
new ToolStripMenuItem("0.25 Second", null, SetCpuTimerInterval)
{
Checked = cpuTimerInterval.Equals(250),
Tag = 250
},
new ToolStripMenuItem("0.05 Second", null, SetCpuTimerInterval)
{
Checked = cpuTimerInterval.Equals(50),
Tag = 50
},
});

ContextMenuStrip contextMenuStrip = new ContextMenuStrip(new Container());
contextMenuStrip.Items.AddRange(new ToolStripItem[]
{
runnerMenu,
themeMenu,
startupMenu,
runnerSpeedLimit,
cpuTimerIntervalMenu,
new ToolStripSeparator(),
new ToolStripMenuItem($"{Application.ProductName} v{Application.ProductVersion}")
{
Expand All @@ -168,9 +199,10 @@ public RunCatApplicationContext()
notifyIcon.DoubleClick += new EventHandler(HandleDoubleClick);

UpdateThemeIcons();
SetAnimation();
SetSpeed();
RefreshCpuTimerInterval();
StartObserveCPU();
AnimationTick();

current = 1;
}
Expand All @@ -179,6 +211,7 @@ private void OnApplicationExit(object sender, EventArgs e)
UserSettings.Default.Runner = runner;
UserSettings.Default.Theme = manualTheme;
UserSettings.Default.Speed = speed;
UserSettings.Default.CpuTimerInterval = cpuTimerInterval;
UserSettings.Default.Save();
}

Expand Down Expand Up @@ -238,6 +271,14 @@ private void UpdateCheckedState(ToolStripMenuItem sender, ToolStripMenuItem menu
sender.Checked = true;
}

private void SetCpuTimerInterval(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
UpdateCheckedState(item, cpuTimerIntervalMenu);
cpuTimerInterval = (int)item.Tag;
RefreshCpuTimerInterval();
}

private void SetRunner(object sender, EventArgs e)
{
ToolStripMenuItem item = (ToolStripMenuItem)sender;
Expand Down Expand Up @@ -328,58 +369,41 @@ private void SetStartup(object sender, EventArgs e)
private void Exit(object sender, EventArgs e)
{
cpuUsage.Close();
animateTimer.Stop();
cpuTimer.Stop();
notifyIcon.Visible = false;
Application.Exit();
}

private void AnimationTick(object sender, EventArgs e)
private async void AnimationTick()
{
if (icons.Length <= current) current = 0;
notifyIcon.Icon = icons[current];
current = (current + 1) % icons.Length;
await Task.Delay((int) interval);
_ = Task.Run(() => AnimationTick()); // Prevents stack overflow by recursive function call
}

private void SetAnimation()
private void CPUTick()
{
animateTimer.Interval = ANIMATE_TIMER_DEFAULT_INTERVAL;
animateTimer.Tick += new EventHandler(AnimationTick);
}
var rawInterval = cpuUsage.NextValue();
notifyIcon.Text = $"CPU: {rawInterval:f1}%";
rawInterval = 200.0f / (float)Math.Max(1.0f, Math.Min(20.0f, rawInterval / 5.0f));

private void CPUTickSpeed()
{
// Apply interval later to prevent using the raw CPU usage value before processing.
if (!speed.Equals("default"))
{
float manualInterval = (float)Math.Max(minCPU, interval);
animateTimer.Stop();
animateTimer.Interval = (int)manualInterval;
animateTimer.Start();
}
interval = Math.Max(minCPU, rawInterval);
else
{
animateTimer.Stop();
animateTimer.Interval = (int)interval;
animateTimer.Start();
}
}

private void CPUTick()
{
interval = Math.Min(100, cpuUsage.NextValue()); // Sometimes got over 100% so it should be limited to 100%
notifyIcon.Text = $"CPU: {interval:f1}%";
interval = 200.0f / (float)Math.Max(1.0f, Math.Min(20.0f, interval / 5.0f));
_ = interval;
CPUTickSpeed();
interval = rawInterval;
}
private void ObserveCPUTick(object sender, EventArgs e)
{
CPUTick();
}

private void RefreshCpuTimerInterval() => cpuTimer.Interval = cpuTimerInterval;

private void StartObserveCPU()
{
cpuTimer.Interval = CPU_TIMER_DEFAULT_INTERVAL;
cpuTimer.Tick += new EventHandler(ObserveCPUTick);
cpuTimer.Start();
}
Expand Down
33 changes: 21 additions & 12 deletions RunCat/Properties/UserSettings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions RunCat/Properties/UserSettings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -11,5 +11,8 @@
<Setting Name="Speed" Type="System.String" Scope="User">
<Value Profile="(Default)">default</Value>
</Setting>
<Setting Name="CpuTimerInterval" Type="System.Int32" Scope="User">
<Value Profile="(Default)">3000</Value>
</Setting>
</Settings>
</SettingsFile>