diff --git a/Dotnet/MainForm.cs b/Dotnet/MainForm.cs index 5f0fe627..87209ebc 100644 --- a/Dotnet/MainForm.cs +++ b/Dotnet/MainForm.cs @@ -6,7 +6,6 @@ using System; using System.Drawing; -using System.IO; using System.Reflection; using System.Windows.Forms; using CefSharp; @@ -19,6 +18,7 @@ public partial class MainForm : WinformBase public static MainForm Instance; private static NLog.Logger jslogger = NLog.LogManager.GetLogger("Javascript"); public ChromiumWebBrowser Browser; + private readonly Timer _saveTimer; private int LastLocationX; private int LastLocationY; private int LastSizeWidth; @@ -29,6 +29,11 @@ public MainForm() Instance = this; InitializeComponent(); + + // adding a 5s delay here to avoid excessive writes to disk + _saveTimer = new Timer(); + _saveTimer.Interval = 5000; + _saveTimer.Tick += SaveTimer_Tick; try { var location = Assembly.GetExecutingAssembly().Location; @@ -133,8 +138,15 @@ private void MainForm_Resize(object sender, System.EventArgs e) } LastSizeWidth = Size.Width; LastSizeHeight = Size.Height; + + _saveTimer.Start(); + } + + private void SaveTimer_Tick(object sender, EventArgs e) + { + SaveWindowState(); + _saveTimer.Stop(); } - private void MainForm_Move(object sender, System.EventArgs e) { if (WindowState != FormWindowState.Normal) @@ -143,6 +155,8 @@ private void MainForm_Move(object sender, System.EventArgs e) } LastLocationX = Location.X; LastLocationY = Location.Y; + + _saveTimer.Start(); } private void MainForm_FormClosing(object sender, FormClosingEventArgs e) @@ -154,14 +168,21 @@ private void MainForm_FormClosing(object sender, FormClosingEventArgs e) Hide(); } } - - private void MainForm_FormClosed(object sender, FormClosedEventArgs e) + + + private void SaveWindowState() { VRCXStorage.Instance.Set("VRCX_LocationX", LastLocationX.ToString()); VRCXStorage.Instance.Set("VRCX_LocationY", LastLocationY.ToString()); VRCXStorage.Instance.Set("VRCX_SizeWidth", LastSizeWidth.ToString()); VRCXStorage.Instance.Set("VRCX_SizeHeight", LastSizeHeight.ToString()); VRCXStorage.Instance.Set("VRCX_WindowState", ((int)WindowState).ToString()); + VRCXStorage.Instance.Flush(); + } + + private void MainForm_FormClosed(object sender, FormClosedEventArgs e) + { + SaveWindowState(); } private void TrayIcon_MouseClick(object sender, MouseEventArgs e) @@ -195,6 +216,7 @@ private void TrayMenu_DevTools_Click(object sender, System.EventArgs e) private void TrayMenu_Quit_Click(object sender, System.EventArgs e) { + SaveWindowState(); Application.Exit(); } }