From c0a38b45f16b882ef782bab06ee7e9fd59dea7e6 Mon Sep 17 00:00:00 2001 From: KrossX Date: Sat, 3 Aug 2019 09:58:51 -0300 Subject: [PATCH] GSDumpGUI: Replaced watchdog thread with Forms.Timer Explicit dispose of watchers and timer on Form dispose, also event enum. --- tools/GSDumpGUI/Core/Program.cs | 10 ++-- tools/GSDumpGUI/Forms/frmMain.Designer.cs | 7 ++- tools/GSDumpGUI/Forms/frmMain.cs | 66 ++++++++++++++--------- 3 files changed, 52 insertions(+), 31 deletions(-) diff --git a/tools/GSDumpGUI/Core/Program.cs b/tools/GSDumpGUI/Core/Program.cs index 149758f992..cb9ef723cd 100644 --- a/tools/GSDumpGUI/Core/Program.cs +++ b/tools/GSDumpGUI/Core/Program.cs @@ -48,7 +48,6 @@ namespace GSDumpGUI static private TreeNode CurrentNode; static public IntPtr hMainIcon; - static public bool isProgRunning; [STAThread] static void Main(String[] args) @@ -118,14 +117,15 @@ namespace GSDumpGUI Server.OnClientAfterConnect += new TCPLibrary.Core.Server.ConnectedHandler(Server_OnClientAfterConnect); Server.OnClientAfterDisconnected += new TCPLibrary.Core.Server.DisconnectedHandler(Server_OnClientAfterDisconnected); Server.Enabled = true; - isProgRunning = true; Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); - frmMain = new GSDumpGUI(); - Application.Run(frmMain); - isProgRunning = false; + using (frmMain = new GSDumpGUI()) + { + Application.Run(frmMain); + } + Server.Enabled = false; } } diff --git a/tools/GSDumpGUI/Forms/frmMain.Designer.cs b/tools/GSDumpGUI/Forms/frmMain.Designer.cs index 89bfc3d135..a7b48e3062 100644 --- a/tools/GSDumpGUI/Forms/frmMain.Designer.cs +++ b/tools/GSDumpGUI/Forms/frmMain.Designer.cs @@ -13,9 +13,12 @@ /// true if managed resources should be disposed; otherwise, false. protected override void Dispose(bool disposing) { - if (disposing && (components != null)) + if (disposing) { - components.Dispose(); + DisposeExtra(); + + if (components != null) + components.Dispose(); } base.Dispose(disposing); } diff --git a/tools/GSDumpGUI/Forms/frmMain.cs b/tools/GSDumpGUI/Forms/frmMain.cs index a43c2a801f..1bad6a874a 100644 --- a/tools/GSDumpGUI/Forms/frmMain.cs +++ b/tools/GSDumpGUI/Forms/frmMain.cs @@ -22,13 +22,11 @@ */ using System; -using System.Collections; using System.Collections.Generic; using System.Collections.Concurrent; using System.ComponentModel; using System.Drawing; using System.Windows.Forms; -using System.Threading; using System.IO; using System.Diagnostics; using System.Linq; @@ -97,7 +95,9 @@ namespace GSDumpGUI private List _dllWatcher; private List _dumpWatcher; - private ConcurrentQueue _watcherEvents; + enum FileChangeEvt { Dll = 1, Dump = 2 }; + private ConcurrentQueue _watcherEvents; + private System.Windows.Forms.Timer _fileChangesWatchdog; private string _gsdxPathOld, _dumpPathOld; @@ -136,41 +136,59 @@ namespace GSDumpGUI _dllWatcher = new List(); _dumpWatcher = new List(); - _watcherEvents = new ConcurrentQueue(); + _watcherEvents = new ConcurrentQueue(); - Thread watcherThread = new Thread(ChangesWatchdogThread); - watcherThread.Start(); + _fileChangesWatchdog = new System.Windows.Forms.Timer(); + _fileChangesWatchdog.Tick += new EventHandler(FileChangesWatchdog); + _fileChangesWatchdog.Interval = 1000; + _fileChangesWatchdog.Start(); } - private void ChangesWatchdogThread() + private void DisposeExtra() { - while (Program.isProgRunning) + foreach (FileSystemWatcher w in _dllWatcher) { - bool dllReload = false; - bool dumpReload = false; - - int evt; - while (_watcherEvents.TryDequeue(out evt)) - { - if (evt == 1) dllReload = true; - else if (evt == 2) dumpReload = true; - } - - if (dllReload) this.Invoke((MethodInvoker)delegate { ReloadGsdxDlls(); }); - if (dumpReload) this.Invoke((MethodInvoker)delegate { ReloadGsdxDumps(); }); - - Thread.Sleep(1000); + w.EnableRaisingEvents = false; + w.Dispose(); } + + foreach (FileSystemWatcher w in _dumpWatcher) + { + w.EnableRaisingEvents = false; + w.Dispose(); + } + + _dllWatcher.Clear(); + _dumpWatcher.Clear(); + + _fileChangesWatchdog.Stop(); + _fileChangesWatchdog.Dispose(); + } + + private void FileChangesWatchdog(object source, EventArgs e) + { + bool dllReload = false; + bool dumpReload = false; + + FileChangeEvt evt; + while (_watcherEvents.TryDequeue(out evt)) + { + if (evt == FileChangeEvt.Dll) dllReload = true; + else if (evt == FileChangeEvt.Dump) dumpReload = true; + } + + if (dllReload) ReloadGsdxDlls(); + if (dumpReload) ReloadGsdxDumps(); } private void OnDllDirChange(object source, FileSystemEventArgs e) { - _watcherEvents.Enqueue(1); + _watcherEvents.Enqueue(FileChangeEvt.Dll); } private void OnDumpDirChange(object source, FileSystemEventArgs e) { - _watcherEvents.Enqueue(2); + _watcherEvents.Enqueue(FileChangeEvt.Dump); } private static void BindListControl(ListControl lb, TModel model, Func> collectionAccessor, Expression> displayTextAccessor, Expression> selectedIndexAccessor)