From f55c4cad9a28157f8ae556e5878f932c23641a15 Mon Sep 17 00:00:00 2001 From: Darren Ohonba - Evans Date: Sat, 25 May 2024 14:25:52 +0100 Subject: [PATCH] Added some error handling. --- AsusFanControlGUI/App.config | 6 ++ AsusFanControlGUI/Form1.Designer.cs | 43 +++++---- AsusFanControlGUI/Form1.cs | 93 +++++++++++++++---- .../Properties/Settings.Designer.cs | 24 +++++ .../Properties/Settings.settings | 6 ++ 5 files changed, 140 insertions(+), 32 deletions(-) diff --git a/AsusFanControlGUI/App.config b/AsusFanControlGUI/App.config index d5531ab..dfcb056 100644 --- a/AsusFanControlGUI/App.config +++ b/AsusFanControlGUI/App.config @@ -40,6 +40,12 @@ 2000 + + False + + + + diff --git a/AsusFanControlGUI/Form1.Designer.cs b/AsusFanControlGUI/Form1.Designer.cs index c8fe912..8dab876 100644 --- a/AsusFanControlGUI/Form1.Designer.cs +++ b/AsusFanControlGUI/Form1.Designer.cs @@ -44,11 +44,12 @@ this.toolStripMenuItemTurnOffControlOnExit = new System.Windows.Forms.ToolStripMenuItem(); this.toolStripMenuItemForbidUnsafeSettings = 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.startupSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.startMinimisedToolStripMenuItem = 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.label5 = new System.Windows.Forms.Label(); this.pictureBoxFanCurve = new System.Windows.Forms.PictureBox(); @@ -198,8 +199,7 @@ this.toolStripMenuItem1.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { this.toolStripMenuItemTurnOffControlOnExit, this.toolStripMenuItemForbidUnsafeSettings, - this.allowFanCurveSettingViaTextToolStripMenuItem, - this.resetToDefaultsToolStripMenuItem}); + this.allowFanCurveSettingViaTextToolStripMenuItem}); this.toolStripMenuItem1.Name = "toolStripMenuItem1"; this.toolStripMenuItem1.Size = new System.Drawing.Size(89, 24); this.toolStripMenuItem1.Text = "Advanced"; @@ -228,20 +228,16 @@ this.allowFanCurveSettingViaTextToolStripMenuItem.Text = "Allow FanCurve Seting via Text"; 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 // this.optionsToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] { - this.startupSettingsToolStripMenuItem}); + this.startupSettingsToolStripMenuItem, + this.restartApplicationToolStripMenuItem, + this.resetToDefaultsToolStripMenuItem}); this.optionsToolStripMenuItem.Name = "optionsToolStripMenuItem"; this.optionsToolStripMenuItem.Size = new System.Drawing.Size(75, 24); this.optionsToolStripMenuItem.Text = "Options"; + this.optionsToolStripMenuItem.Click += new System.EventHandler(this.optionsToolStripMenuItem_Click); // // startupSettingsToolStripMenuItem // @@ -249,14 +245,14 @@ this.startMinimisedToolStripMenuItem, this.startWithWindowsToolStripMenuItem}); this.startupSettingsToolStripMenuItem.Name = "startupSettingsToolStripMenuItem"; - this.startupSettingsToolStripMenuItem.Size = new System.Drawing.Size(197, 26); - this.startupSettingsToolStripMenuItem.Text = "Startup Settings"; + this.startupSettingsToolStripMenuItem.Size = new System.Drawing.Size(224, 26); + this.startupSettingsToolStripMenuItem.Text = "Startup settings"; // // startMinimisedToolStripMenuItem // this.startMinimisedToolStripMenuItem.CheckOnClick = true; 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.Click += new System.EventHandler(this.startMinimisedToolStripMenuItem_Click); // @@ -264,10 +260,24 @@ // this.startWithWindowsToolStripMenuItem.CheckOnClick = true; 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.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 // this.toolStripMenuItemCheckForUpdates.Name = "toolStripMenuItemCheckForUpdates"; @@ -618,7 +628,6 @@ private System.Windows.Forms.TextBox textBox1; private System.Windows.Forms.Button button3; private System.Windows.Forms.Button button4; - private System.Windows.Forms.ToolStripMenuItem resetToDefaultsToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem allowFanCurveSettingViaTextToolStripMenuItem; private System.Windows.Forms.Label label10; private System.Windows.Forms.ToolStripMenuItem optionsToolStripMenuItem; @@ -626,6 +635,8 @@ private System.Windows.Forms.ToolStripMenuItem startMinimisedToolStripMenuItem; private System.Windows.Forms.ToolStripMenuItem startWithWindowsToolStripMenuItem; private System.Windows.Forms.Label label11; + private System.Windows.Forms.ToolStripMenuItem restartApplicationToolStripMenuItem; + private System.Windows.Forms.ToolStripMenuItem resetToDefaultsToolStripMenuItem; } } diff --git a/AsusFanControlGUI/Form1.cs b/AsusFanControlGUI/Form1.cs index 2220c42..3b0a462 100644 --- a/AsusFanControlGUI/Form1.cs +++ b/AsusFanControlGUI/Form1.cs @@ -25,6 +25,7 @@ namespace AsusFanControlGUI private readonly Random rnd = new Random(); readonly AsusControl asusControl = new AsusControl(); int currentFanSpeed = 0; + ulong currentTemp = 0; public Form1() { @@ -41,6 +42,8 @@ namespace AsusFanControlGUI { if (IsHandleCreated) { + startErrorHandler(); + toolStripMenuItemTurnOffControlOnExit.Checked = Properties.Settings.Default.turnOffControlOnExit; toolStripMenuItemForbidUnsafeSettings.Checked = Properties.Settings.Default.forbidUnsafeSettings; 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() { if (WindowState == FormWindowState.Minimized) @@ -98,6 +125,8 @@ namespace AsusFanControlGUI // Get the results from the completed tasks labelRPM.Text = fanSpeedsTask.Result; labelCPUTemp.Text = cpuTempTask.Result; + currentTemp = (ulong)Decimal.Parse(cpuTempTask.Result); + startErrorHandler(); await Task.Delay(250); Timer_Tick(); @@ -237,8 +266,10 @@ namespace AsusFanControlGUI private async void button2_Click(object sender, EventArgs e) { - ulong currentTemp = await Task.Run(() => asusControl.Thermal_Read_Cpu_Temperature()); - labelCPUTemp.Text = $"{currentTemp}"; + ulong temp = await Task.Run(() => asusControl.Thermal_Read_Cpu_Temperature()); + currentTemp = temp; + labelCPUTemp.Text = $"{temp}"; + startErrorHandler(); } // My Code: @@ -527,14 +558,16 @@ namespace AsusFanControlGUI //Console.WriteLine("Fan Curve, " + (int)numericUpDown2.Value); // 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); - - double fanSpeed = CalculateFanSpeed(currentTemp); + + double fanSpeed = CalculateFanSpeed(temp); // Apply hysteresis to prevent rapid fan speed changes 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 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}"); if (fanSpeed != 0) { - label3.Text = $"Set fan speed to {(int)fanSpeed}%, current temp: {currentTemp}°C";// (Stamp: {rnd.Next(1000)})"; - notifyIcon1.Text = $"AsusFanControlEnhanced - Current Temp: {(int)currentTemp}°C - Fan Speed: {(int)fanSpeed}%"; + label3.Text = $"Set fan speed to {(int)fanSpeed}%, current temp: {temp}°C";// (Stamp: {rnd.Next(1000)})"; + 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(); WindowState = FormWindowState.Normal; notifyIcon1.Visible = false; + mayShowError(); } private void closeToolStripMenuItem_Click(object sender, EventArgs e) @@ -594,6 +628,7 @@ namespace AsusFanControlGUI Show(); WindowState = FormWindowState.Normal; notifyIcon1.Visible = false; + mayShowError(); } } @@ -665,13 +700,6 @@ namespace AsusFanControlGUI 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) { if (e.KeyChar != (char)Keys.Enter) @@ -747,6 +775,21 @@ namespace AsusFanControlGUI if (startMinimized) { 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(); } + 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()}"; } } } diff --git a/AsusFanControlGUI/Properties/Settings.Designer.cs b/AsusFanControlGUI/Properties/Settings.Designer.cs index f3308f4..711b539 100644 --- a/AsusFanControlGUI/Properties/Settings.Designer.cs +++ b/AsusFanControlGUI/Properties/Settings.Designer.cs @@ -142,5 +142,29 @@ namespace AsusFanControlGUI.Properties { 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; + } + } } } diff --git a/AsusFanControlGUI/Properties/Settings.settings b/AsusFanControlGUI/Properties/Settings.settings index be921f4..e852e05 100644 --- a/AsusFanControlGUI/Properties/Settings.settings +++ b/AsusFanControlGUI/Properties/Settings.settings @@ -32,5 +32,11 @@ 2000 + + False + + + + \ No newline at end of file