From 8c6c61d3e31e0c35b5b1deb18f8689036fc9a047 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Michael=20M=C3=B6ller?= Date: Tue, 25 May 2010 22:33:03 +0000 Subject: [PATCH] Added an event handler to save the configuration when the user logs off without closing the application first (http://blogs.msdn.com/b/oldnewthing/archive/2008/04/21/8413175.aspx), because FormClosed is not called in that case. --- GUI/CrashReportForm.cs | 1 - GUI/MainForm.cs | 24 +++++++++++++++++------- OpenHardwareMonitor.csproj | 2 -- Properties/AssemblyInfo.cs | 4 ++-- Utilities/Config.cs | 6 +++++- 5 files changed, 24 insertions(+), 13 deletions(-) diff --git a/GUI/CrashReportForm.cs b/GUI/CrashReportForm.cs index c07a2f3..3a60816 100644 --- a/GUI/CrashReportForm.cs +++ b/GUI/CrashReportForm.cs @@ -38,7 +38,6 @@ using System; using System.Collections.Generic; using System.ComponentModel; -using System.Data; using System.Drawing; using System.IO; using System.Net; diff --git a/GUI/MainForm.cs b/GUI/MainForm.cs index 2d325d5..668591c 100644 --- a/GUI/MainForm.cs +++ b/GUI/MainForm.cs @@ -173,8 +173,14 @@ namespace OpenHardwareMonitor.GUI { // Create a handle, otherwise calling Close() does not fire FormClosed IntPtr handle = Handle; - } + // Make sure the settings are saved when the user logs off + Microsoft.Win32.SystemEvents.SessionEnded += + delegate(object sender, Microsoft.Win32.SessionEndedEventArgs e) { + SaveConfiguration(); + }; + } + private void SubHardwareAdded(IHardware hardware, Node node) { Node hardwareNode = new HardwareNode(hardware); node.Nodes.Add(hardwareNode); @@ -262,8 +268,7 @@ namespace OpenHardwareMonitor.GUI { sensorSystemTray.Redraw(); } - private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { - + private void SaveConfiguration() { Config.Set(hiddenMenuItem.Name, hiddenMenuItem.Checked); Config.Set(plotMenuItem.Name, plotMenuItem.Checked); @@ -274,7 +279,7 @@ namespace OpenHardwareMonitor.GUI { Config.Set(startMinMenuItem.Name, startMinMenuItem.Checked); Config.Set(minTrayMenuItem.Name, minTrayMenuItem.Checked); - Config.Set(hddMenuItem.Name, hddMenuItem.Checked); + Config.Set(hddMenuItem.Name, hddMenuItem.Checked); if (WindowState != FormWindowState.Minimized) { Config.Set("mainForm.Location.X", Location.X); @@ -283,12 +288,17 @@ namespace OpenHardwareMonitor.GUI { Config.Set("mainForm.Height", Height); } - foreach (TreeColumn column in treeView.Columns) - Config.Set("treeView.Columns." + column.Header + ".Width", + foreach (TreeColumn column in treeView.Columns) + Config.Set("treeView.Columns." + column.Header + ".Width", column.Width); - timer.Enabled = false; + Config.Save(); + } + private void MainForm_FormClosed(object sender, FormClosedEventArgs e) { + SaveConfiguration(); + + timer.Enabled = false; sensorSystemTray.Dispose(); notifyIcon.Dispose(); computer.Close(); diff --git a/OpenHardwareMonitor.csproj b/OpenHardwareMonitor.csproj index a0cda10..e99ee8f 100644 --- a/OpenHardwareMonitor.csproj +++ b/OpenHardwareMonitor.csproj @@ -52,12 +52,10 @@ - - diff --git a/Properties/AssemblyInfo.cs b/Properties/AssemblyInfo.cs index 7fe2346..2211328 100644 --- a/Properties/AssemblyInfo.cs +++ b/Properties/AssemblyInfo.cs @@ -69,5 +69,5 @@ using System.Runtime.InteropServices; // You can specify all the values or you can default the Build and Revision Numbers // by using the '*' as shown below: // [assembly: AssemblyVersion("1.0.*")] -[assembly: AssemblyVersion("0.1.35.0")] -[assembly: AssemblyFileVersion("0.1.35.0")] +[assembly: AssemblyVersion("0.1.36.0")] +[assembly: AssemblyFileVersion("0.1.36.0")] diff --git a/Utilities/Config.cs b/Utilities/Config.cs index 1efdb3a..ab5e9a8 100644 --- a/Utilities/Config.cs +++ b/Utilities/Config.cs @@ -60,7 +60,7 @@ namespace OpenHardwareMonitor.Utilities { System.Configuration.ConfigurationUserLevel.None); } - ~Config() { + private void SaveConfig() { string tempName = Path.ChangeExtension(fileName, ".tmp"); if (File.Exists(tempName)) @@ -73,6 +73,10 @@ namespace OpenHardwareMonitor.Utilities { } catch (System.Configuration.ConfigurationErrorsException) { } } + public static void Save() { + instance.SaveConfig(); + } + public static Config Settings { get { return instance;