Added some error handling.

This commit is contained in:
Darren Ohonba - Evans
2024-05-25 14:25:52 +01:00
parent 5d2b6543be
commit f55c4cad9a
5 changed files with 140 additions and 32 deletions

View File

@@ -40,6 +40,12 @@
<setting name="updateSpeed" serializeAs="String"> <setting name="updateSpeed" serializeAs="String">
<value>2000</value> <value>2000</value>
</setting> </setting>
<setting name="wasError" serializeAs="String">
<value>False</value>
</setting>
<setting name="errorMsg" serializeAs="String">
<value />
</setting>
</AsusFanControlGUI.Properties.Settings> </AsusFanControlGUI.Properties.Settings>
</userSettings> </userSettings>
</configuration> </configuration>

View File

@@ -44,11 +44,12 @@
this.toolStripMenuItemTurnOffControlOnExit = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemTurnOffControlOnExit = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItemForbidUnsafeSettings = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemForbidUnsafeSettings = new System.Windows.Forms.ToolStripMenuItem();
this.allowFanCurveSettingViaTextToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.allowFanCurveSettingViaTextToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.resetToDefaultsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.optionsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.optionsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.startupSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.startupSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.startMinimisedToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.startMinimisedToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.startWithWindowsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.startWithWindowsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.restartApplicationToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.resetToDefaultsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripMenuItemCheckForUpdates = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemCheckForUpdates = new System.Windows.Forms.ToolStripMenuItem();
this.label5 = new System.Windows.Forms.Label(); this.label5 = new System.Windows.Forms.Label();
this.pictureBoxFanCurve = new System.Windows.Forms.PictureBox(); this.pictureBoxFanCurve = new System.Windows.Forms.PictureBox();
@@ -198,8 +199,7 @@
this.toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.toolStripMenuItemTurnOffControlOnExit, this.toolStripMenuItemTurnOffControlOnExit,
this.toolStripMenuItemForbidUnsafeSettings, this.toolStripMenuItemForbidUnsafeSettings,
this.allowFanCurveSettingViaTextToolStripMenuItem, this.allowFanCurveSettingViaTextToolStripMenuItem});
this.resetToDefaultsToolStripMenuItem});
this.toolStripMenuItem1.Name = "toolStripMenuItem1"; this.toolStripMenuItem1.Name = "toolStripMenuItem1";
this.toolStripMenuItem1.Size = new System.Drawing.Size(89, 24); this.toolStripMenuItem1.Size = new System.Drawing.Size(89, 24);
this.toolStripMenuItem1.Text = "Advanced"; this.toolStripMenuItem1.Text = "Advanced";
@@ -228,20 +228,16 @@
this.allowFanCurveSettingViaTextToolStripMenuItem.Text = "Allow FanCurve Seting via Text"; this.allowFanCurveSettingViaTextToolStripMenuItem.Text = "Allow FanCurve Seting via Text";
this.allowFanCurveSettingViaTextToolStripMenuItem.Click += new System.EventHandler(this.allowFanCurveSettingViaTextToolStripMenuItem_Click); this.allowFanCurveSettingViaTextToolStripMenuItem.Click += new System.EventHandler(this.allowFanCurveSettingViaTextToolStripMenuItem_Click);
// //
// resetToDefaultsToolStripMenuItem
//
this.resetToDefaultsToolStripMenuItem.Name = "resetToDefaultsToolStripMenuItem";
this.resetToDefaultsToolStripMenuItem.Size = new System.Drawing.Size(293, 26);
this.resetToDefaultsToolStripMenuItem.Text = "Reset to defaults";
this.resetToDefaultsToolStripMenuItem.Click += new System.EventHandler(this.resetToDefaultsToolStripMenuItem_Click);
//
// optionsToolStripMenuItem // optionsToolStripMenuItem
// //
this.optionsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.optionsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.startupSettingsToolStripMenuItem}); this.startupSettingsToolStripMenuItem,
this.restartApplicationToolStripMenuItem,
this.resetToDefaultsToolStripMenuItem});
this.optionsToolStripMenuItem.Name = "optionsToolStripMenuItem"; this.optionsToolStripMenuItem.Name = "optionsToolStripMenuItem";
this.optionsToolStripMenuItem.Size = new System.Drawing.Size(75, 24); this.optionsToolStripMenuItem.Size = new System.Drawing.Size(75, 24);
this.optionsToolStripMenuItem.Text = "Options"; this.optionsToolStripMenuItem.Text = "Options";
this.optionsToolStripMenuItem.Click += new System.EventHandler(this.optionsToolStripMenuItem_Click);
// //
// startupSettingsToolStripMenuItem // startupSettingsToolStripMenuItem
// //
@@ -249,14 +245,14 @@
this.startMinimisedToolStripMenuItem, this.startMinimisedToolStripMenuItem,
this.startWithWindowsToolStripMenuItem}); this.startWithWindowsToolStripMenuItem});
this.startupSettingsToolStripMenuItem.Name = "startupSettingsToolStripMenuItem"; this.startupSettingsToolStripMenuItem.Name = "startupSettingsToolStripMenuItem";
this.startupSettingsToolStripMenuItem.Size = new System.Drawing.Size(197, 26); this.startupSettingsToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.startupSettingsToolStripMenuItem.Text = "Startup Settings"; this.startupSettingsToolStripMenuItem.Text = "Startup settings";
// //
// startMinimisedToolStripMenuItem // startMinimisedToolStripMenuItem
// //
this.startMinimisedToolStripMenuItem.CheckOnClick = true; this.startMinimisedToolStripMenuItem.CheckOnClick = true;
this.startMinimisedToolStripMenuItem.Name = "startMinimisedToolStripMenuItem"; this.startMinimisedToolStripMenuItem.Name = "startMinimisedToolStripMenuItem";
this.startMinimisedToolStripMenuItem.Size = new System.Drawing.Size(220, 26); this.startMinimisedToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.startMinimisedToolStripMenuItem.Text = "Start Minimised"; this.startMinimisedToolStripMenuItem.Text = "Start Minimised";
this.startMinimisedToolStripMenuItem.Click += new System.EventHandler(this.startMinimisedToolStripMenuItem_Click); this.startMinimisedToolStripMenuItem.Click += new System.EventHandler(this.startMinimisedToolStripMenuItem_Click);
// //
@@ -264,10 +260,24 @@
// //
this.startWithWindowsToolStripMenuItem.CheckOnClick = true; this.startWithWindowsToolStripMenuItem.CheckOnClick = true;
this.startWithWindowsToolStripMenuItem.Name = "startWithWindowsToolStripMenuItem"; this.startWithWindowsToolStripMenuItem.Name = "startWithWindowsToolStripMenuItem";
this.startWithWindowsToolStripMenuItem.Size = new System.Drawing.Size(220, 26); this.startWithWindowsToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.startWithWindowsToolStripMenuItem.Text = "Start with Windows"; this.startWithWindowsToolStripMenuItem.Text = "Start with Windows";
this.startWithWindowsToolStripMenuItem.Click += new System.EventHandler(this.startWithWindowsToolStripMenuItem1_Click_1); this.startWithWindowsToolStripMenuItem.Click += new System.EventHandler(this.startWithWindowsToolStripMenuItem1_Click_1);
// //
// restartApplicationToolStripMenuItem
//
this.restartApplicationToolStripMenuItem.Name = "restartApplicationToolStripMenuItem";
this.restartApplicationToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.restartApplicationToolStripMenuItem.Text = "Restart application";
this.restartApplicationToolStripMenuItem.Click += new System.EventHandler(this.restartApplicationToolStripMenuItem_Click);
//
// resetToDefaultsToolStripMenuItem
//
this.resetToDefaultsToolStripMenuItem.Name = "resetToDefaultsToolStripMenuItem";
this.resetToDefaultsToolStripMenuItem.Size = new System.Drawing.Size(224, 26);
this.resetToDefaultsToolStripMenuItem.Text = "Reset to defaults";
this.resetToDefaultsToolStripMenuItem.Click += new System.EventHandler(this.resetToDefaultsToolStripMenuItem_Click);
//
// toolStripMenuItemCheckForUpdates // toolStripMenuItemCheckForUpdates
// //
this.toolStripMenuItemCheckForUpdates.Name = "toolStripMenuItemCheckForUpdates"; this.toolStripMenuItemCheckForUpdates.Name = "toolStripMenuItemCheckForUpdates";
@@ -618,7 +628,6 @@
private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.TextBox textBox1;
private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button3;
private System.Windows.Forms.Button button4; private System.Windows.Forms.Button button4;
private System.Windows.Forms.ToolStripMenuItem resetToDefaultsToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem allowFanCurveSettingViaTextToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem allowFanCurveSettingViaTextToolStripMenuItem;
private System.Windows.Forms.Label label10; private System.Windows.Forms.Label label10;
private System.Windows.Forms.ToolStripMenuItem optionsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem optionsToolStripMenuItem;
@@ -626,6 +635,8 @@
private System.Windows.Forms.ToolStripMenuItem startMinimisedToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem startMinimisedToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem startWithWindowsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem startWithWindowsToolStripMenuItem;
private System.Windows.Forms.Label label11; private System.Windows.Forms.Label label11;
private System.Windows.Forms.ToolStripMenuItem restartApplicationToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem resetToDefaultsToolStripMenuItem;
} }
} }

View File

@@ -25,6 +25,7 @@ namespace AsusFanControlGUI
private readonly Random rnd = new Random(); private readonly Random rnd = new Random();
readonly AsusControl asusControl = new AsusControl(); readonly AsusControl asusControl = new AsusControl();
int currentFanSpeed = 0; int currentFanSpeed = 0;
ulong currentTemp = 0;
public Form1() public Form1()
{ {
@@ -41,6 +42,8 @@ namespace AsusFanControlGUI
{ {
if (IsHandleCreated) if (IsHandleCreated)
{ {
startErrorHandler();
toolStripMenuItemTurnOffControlOnExit.Checked = Properties.Settings.Default.turnOffControlOnExit; toolStripMenuItemTurnOffControlOnExit.Checked = Properties.Settings.Default.turnOffControlOnExit;
toolStripMenuItemForbidUnsafeSettings.Checked = Properties.Settings.Default.forbidUnsafeSettings; toolStripMenuItemForbidUnsafeSettings.Checked = Properties.Settings.Default.forbidUnsafeSettings;
startMinimisedToolStripMenuItem.Checked = Properties.Settings.Default.startMinimised; startMinimisedToolStripMenuItem.Checked = Properties.Settings.Default.startMinimised;
@@ -77,6 +80,30 @@ namespace AsusFanControlGUI
} }
} }
private async void startErrorHandler()
{
int minTemp = 1;
int maxTemp = 200;
Console.WriteLine("Running");
if ((fanCurve.Checked || fanControl.Checked) && (currentTemp < (ulong)minTemp || currentTemp > (ulong)maxTemp))
{
// Give it a second chance
await Task.Delay(1000);
ulong temp = await Task.Run(() => asusControl.Thermal_Read_Cpu_Temperature());
if (temp >= (ulong)minTemp && temp < (ulong)maxTemp)
{
return;
}
Properties.Settings.Default.wasError = true;
Properties.Settings.Default.errorMsg = $"CPU temprature were outside of good range at {currentTemp}°C, either something has not loaded properly or CPU sensors are faulty.";
Properties.Settings.Default.Save();
Console.WriteLine("Restarting");
Application.Restart();
Environment.Exit(0);
}
}
private async void Timer_Tick() private async void Timer_Tick()
{ {
if (WindowState == FormWindowState.Minimized) if (WindowState == FormWindowState.Minimized)
@@ -98,6 +125,8 @@ namespace AsusFanControlGUI
// Get the results from the completed tasks // Get the results from the completed tasks
labelRPM.Text = fanSpeedsTask.Result; labelRPM.Text = fanSpeedsTask.Result;
labelCPUTemp.Text = cpuTempTask.Result; labelCPUTemp.Text = cpuTempTask.Result;
currentTemp = (ulong)Decimal.Parse(cpuTempTask.Result);
startErrorHandler();
await Task.Delay(250); await Task.Delay(250);
Timer_Tick(); Timer_Tick();
@@ -237,8 +266,10 @@ namespace AsusFanControlGUI
private async void button2_Click(object sender, EventArgs e) private async void button2_Click(object sender, EventArgs e)
{ {
ulong currentTemp = await Task.Run(() => asusControl.Thermal_Read_Cpu_Temperature()); ulong temp = await Task.Run(() => asusControl.Thermal_Read_Cpu_Temperature());
labelCPUTemp.Text = $"{currentTemp}"; currentTemp = temp;
labelCPUTemp.Text = $"{temp}";
startErrorHandler();
} }
// My Code: // My Code:
@@ -527,14 +558,16 @@ namespace AsusFanControlGUI
//Console.WriteLine("Fan Curve, " + (int)numericUpDown2.Value); //Console.WriteLine("Fan Curve, " + (int)numericUpDown2.Value);
// Read the current temperature // Read the current temperature
ulong currentTemp = await Task.Run(() => asusControl.Thermal_Read_Cpu_Temperature()); // Implement the ReadTemperature method to get the current temperature ulong temp = await Task.Run(() => asusControl.Thermal_Read_Cpu_Temperature()); // Implement the ReadTemperature method to get the current temperature
currentTemp = temp;
startErrorHandler();
//Console.WriteLine("Temp, " + currentTemp); //Console.WriteLine("Temp, " + currentTemp);
double fanSpeed = CalculateFanSpeed(currentTemp); double fanSpeed = CalculateFanSpeed(temp);
// Apply hysteresis to prevent rapid fan speed changes // Apply hysteresis to prevent rapid fan speed changes
int hysteresis = (int)numericUpDown1.Value; // Adjust the hysteresis value as needed int hysteresis = (int)numericUpDown1.Value; // Adjust the hysteresis value as needed
if ((int)currentTemp > lastTemperature + hysteresis || (int)currentTemp < lastTemperature - hysteresis || fanSpeed < 10) if ((int)temp > lastTemperature + hysteresis || (int)temp < lastTemperature - hysteresis || fanSpeed < 10)
{ {
// Update the fan speed // Update the fan speed
fanSpeed = Math.Max(0, Math.Min(100, fanSpeed)); fanSpeed = Math.Max(0, Math.Min(100, fanSpeed));
@@ -543,10 +576,10 @@ namespace AsusFanControlGUI
Console.WriteLine($"Set fan speed to {(int)fanSpeed}% {rnd.Next(1000)}, last fan speed = {lastTemperature}"); Console.WriteLine($"Set fan speed to {(int)fanSpeed}% {rnd.Next(1000)}, last fan speed = {lastTemperature}");
if (fanSpeed != 0) if (fanSpeed != 0)
{ {
label3.Text = $"Set fan speed to {(int)fanSpeed}%, current temp: {currentTemp}°C";// (Stamp: {rnd.Next(1000)})"; label3.Text = $"Set fan speed to {(int)fanSpeed}%, current temp: {temp}°C";// (Stamp: {rnd.Next(1000)})";
notifyIcon1.Text = $"AsusFanControlEnhanced - Current Temp: {(int)currentTemp}°C - Fan Speed: {(int)fanSpeed}%"; notifyIcon1.Text = $"AsusFanControlEnhanced - Current Temp: {(int)temp}°C - Fan Speed: {(int)fanSpeed}%";
} }
lastTemperature = (int)currentTemp; lastTemperature = (int)temp;
} }
@@ -580,6 +613,7 @@ namespace AsusFanControlGUI
Show(); Show();
WindowState = FormWindowState.Normal; WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false; notifyIcon1.Visible = false;
mayShowError();
} }
private void closeToolStripMenuItem_Click(object sender, EventArgs e) private void closeToolStripMenuItem_Click(object sender, EventArgs e)
@@ -594,6 +628,7 @@ namespace AsusFanControlGUI
Show(); Show();
WindowState = FormWindowState.Normal; WindowState = FormWindowState.Normal;
notifyIcon1.Visible = false; notifyIcon1.Visible = false;
mayShowError();
} }
} }
@@ -665,13 +700,6 @@ namespace AsusFanControlGUI
trackBarSetFanSpeed(); trackBarSetFanSpeed();
} }
private void resetToDefaultsToolStripMenuItem_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Reset();
Application.Restart();
Environment.Exit(0);
}
private void textBox1_KeyPress(object sender, KeyPressEventArgs e) private void textBox1_KeyPress(object sender, KeyPressEventArgs e)
{ {
if (e.KeyChar != (char)Keys.Enter) if (e.KeyChar != (char)Keys.Enter)
@@ -747,6 +775,21 @@ namespace AsusFanControlGUI
if (startMinimized) if (startMinimized)
{ {
MinimizeToTray(); MinimizeToTray();
return;
}
mayShowError();
}
private void mayShowError()
{
Console.WriteLine($"asdad: {Properties.Settings.Default.wasError}");
if (Properties.Settings.Default.wasError == true)
{
MessageBox.Show($"An error caused the application to restart:\n\nError: {Properties.Settings.Default.errorMsg}");
Properties.Settings.Default.wasError = false;
Properties.Settings.Default.errorMsg = "";
Properties.Settings.Default.Save();
} }
} }
@@ -773,6 +816,24 @@ namespace AsusFanControlGUI
Properties.Settings.Default.Save(); Properties.Settings.Default.Save();
} }
private void resetToDefaultsToolStripMenuItem_Click(object sender, EventArgs e)
{
Properties.Settings.Default.Reset();
Application.Restart();
Environment.Exit(0);
}
private void restartApplicationToolStripMenuItem_Click(object sender, EventArgs e)
{
Application.Restart();
Environment.Exit(0);
}
private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
{
}
//notifyIcon1.BalloonTipText = string.Join(" ", asusControl.GetFanSpeeds()) + $" Temp: {asusControl.Thermal_Read_Cpu_Temperature()}"; } //notifyIcon1.BalloonTipText = string.Join(" ", asusControl.GetFanSpeeds()) + $" Temp: {asusControl.Thermal_Read_Cpu_Temperature()}"; }
} }
} }

View File

@@ -142,5 +142,29 @@ namespace AsusFanControlGUI.Properties {
this["updateSpeed"] = value; this["updateSpeed"] = value;
} }
} }
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("False")]
public bool wasError {
get {
return ((bool)(this["wasError"]));
}
set {
this["wasError"] = value;
}
}
[global::System.Configuration.UserScopedSettingAttribute()]
[global::System.Diagnostics.DebuggerNonUserCodeAttribute()]
[global::System.Configuration.DefaultSettingValueAttribute("")]
public string errorMsg {
get {
return ((string)(this["errorMsg"]));
}
set {
this["errorMsg"] = value;
}
}
} }
} }

View File

@@ -32,5 +32,11 @@
<Setting Name="updateSpeed" Type="System.Int32" Scope="User"> <Setting Name="updateSpeed" Type="System.Int32" Scope="User">
<Value Profile="(Default)">2000</Value> <Value Profile="(Default)">2000</Value>
</Setting> </Setting>
<Setting Name="wasError" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="errorMsg" Type="System.String" Scope="User">
<Value Profile="(Default)" />
</Setting>
</Settings> </Settings>
</SettingsFile> </SettingsFile>