From 914ee2cc061f8da6cc0dd61e616b9793f61ddb07 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Thu, 9 Sep 2021 20:53:41 +1200 Subject: [PATCH] Initial partial creation of cancel logic This code eventually will be used to allow the user to cancel waiting for the game/app. This won't work at the moment though as we actually monitor for the game on the UI thread (a big no no). I'll have to fix this in an upcoming update, but for now we'll leave it here. --- DisplayMagician/ShortcutRepository.cs | 32 +++++++++++++++++-- .../UIForms/ShortcutLibraryForm.Designer.cs | 5 +-- .../UIForms/ShortcutLibraryForm.cs | 12 +++++++ 3 files changed, 44 insertions(+), 5 deletions(-) diff --git a/DisplayMagician/ShortcutRepository.cs b/DisplayMagician/ShortcutRepository.cs index 9833ed0..d8bc37a 100644 --- a/DisplayMagician/ShortcutRepository.cs +++ b/DisplayMagician/ShortcutRepository.cs @@ -31,6 +31,7 @@ namespace DisplayMagician //public static Dictionary _shortcutWarningLookup = new Dictionary(); //public static Dictionary _shortcutErrorLookup = new Dictionary(); private static bool _shortcutsLoaded = false; + private static bool _cancelWait = false; // Other constants that are useful private static string AppShortcutStoragePath = Path.Combine(Program.AppDataPath, $"Shortcuts"); private static string _shortcutStorageJsonFileName = Path.Combine(AppShortcutStoragePath, $"Shortcuts_{Version.ToString(2)}.json"); @@ -97,6 +98,11 @@ namespace DisplayMagician get => new Version(1, 0, 0); } + public static bool CancelWait { + get => _cancelWait; + set => _cancelWait = value; + } + #endregion #region Class Methods @@ -1448,9 +1454,15 @@ namespace DisplayMagician break; } + if (_cancelWait) + { + logger.Debug($"ShortcutRepository/RunShortcut: User requested we stop waiting. Exiting loop while waiting for {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} to close."); + break; + } + // Send a message to windows so that it doesn't think // we're locked and try to kill us - System.Threading.Thread.CurrentThread.Join(0); + Thread.CurrentThread.Join(0); Thread.Sleep(1000); } logger.Debug($"ShortcutRepository/RunShortcut: {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} has exited."); @@ -1511,9 +1523,17 @@ namespace DisplayMagician break; } + if (_cancelWait) + { + logger.Debug($"ShortcutRepository/RunShortcut: User requested we stop waiting. Exiting loop while waiting for {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} to close."); + break; + } + + // Send a message to windows so that it doesn't think // we're locked and try to kill us - System.Threading.Thread.CurrentThread.Join(0); + Thread.CurrentThread.Join(0); + // Pause for a second Thread.Sleep(1000); } logger.Debug($"ShortcutRepository/RunShortcut: Alternative Game Executable {altGameProcessToMonitor} has exited."); @@ -1635,9 +1655,15 @@ namespace DisplayMagician break; } + if (_cancelWait) + { + logger.Debug($"ShortcutRepository/RunShortcut: User requested we stop waiting. Exiting loop while waiting for {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} to close."); + break; + } + // Send a message to windows so that it doesn't think // we're locked and try to kill us - System.Threading.Thread.CurrentThread.Join(0); + Thread.CurrentThread.Join(0); Thread.Sleep(1000); } logger.Debug($"ShortcutRepository/RunShortcut: {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} has exited."); diff --git a/DisplayMagician/UIForms/ShortcutLibraryForm.Designer.cs b/DisplayMagician/UIForms/ShortcutLibraryForm.Designer.cs index 2b8a414..8920355 100644 --- a/DisplayMagician/UIForms/ShortcutLibraryForm.Designer.cs +++ b/DisplayMagician/UIForms/ShortcutLibraryForm.Designer.cs @@ -246,7 +246,7 @@ this.tsmi_delete.Text = "Delete Shortcut..."; this.tsmi_delete.Click += new System.EventHandler(this.tsmi_delete_Click); // - // lbl_masked_form + // lbl_mask // this.lbl_mask.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) @@ -255,7 +255,7 @@ this.lbl_mask.Font = new System.Drawing.Font("Microsoft Sans Serif", 16F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.lbl_mask.ForeColor = System.Drawing.Color.White; this.lbl_mask.Location = new System.Drawing.Point(360, 306); - this.lbl_mask.Name = "lbl_masked_form"; + this.lbl_mask.Name = "lbl_mask"; this.lbl_mask.Size = new System.Drawing.Size(415, 104); this.lbl_mask.TabIndex = 33; this.lbl_mask.Text = "lbl_masked_form"; @@ -289,6 +289,7 @@ this.Text = "DisplayMagician - Game Shortcuts"; this.Activated += new System.EventHandler(this.ShortcutLibraryForm_Activated); this.Load += new System.EventHandler(this.ShortcutLibraryForm_Load); + this.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.ShortcutLibraryForm_KeyPress); this.cms_shortcuts.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); diff --git a/DisplayMagician/UIForms/ShortcutLibraryForm.cs b/DisplayMagician/UIForms/ShortcutLibraryForm.cs index 228817b..263caac 100644 --- a/DisplayMagician/UIForms/ShortcutLibraryForm.cs +++ b/DisplayMagician/UIForms/ShortcutLibraryForm.cs @@ -468,5 +468,17 @@ namespace DisplayMagician.UIForms { btn_delete.PerformClick(); } + + private void ShortcutLibraryForm_KeyPress(object sender, KeyPressEventArgs e) + { + if (lbl_mask.Visible == true) + { + if (e.KeyChar == 27) + { + // We set the CancelWait to be true on ShortcutRespository, and it will be picked up bythe shortcut check. + ShortcutRepository.CancelWait = true; + } + } + } } }