From dce0ed825289ffa37554121a2af70e7abec2bbb7 Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Sun, 22 Nov 2020 09:26:17 +1300 Subject: [PATCH] Fixed application process monitoring Stopped the application threat monitoring halting the UI completely, which fixed some UI updating issues. Now UI thread continues to poll waiting for appl/game to exit, but does it in a loop rather than blocking using WaitForExit(). Means that the notification icon gets updated and the UI gets a MaskedForm over it. --- HeliosPlus/Resources/Language.Designer.cs | 6 ++-- HeliosPlus/Resources/Language.resx | 4 +-- HeliosPlus/ShortcutRepository.cs | 40 +++++++++++++++------ HeliosPlus/UIForms/ShortcutForm.Designer.cs | 34 +++++++++--------- HeliosPlus/UIForms/ShortcutForm.cs | 13 +++---- 5 files changed, 58 insertions(+), 39 deletions(-) diff --git a/HeliosPlus/Resources/Language.Designer.cs b/HeliosPlus/Resources/Language.Designer.cs index 7f3f2a0..ad3cc9c 100644 --- a/HeliosPlus/Resources/Language.Designer.cs +++ b/HeliosPlus/Resources/Language.Designer.cs @@ -593,11 +593,11 @@ namespace HeliosPlus.Resources { } /// - /// Looks up a localized string similar to Selected file is not a valid executable file.. + /// Looks up a localized string similar to Selected file is not a valid file.. /// - internal static string Selected_file_is_not_a_valid_executable_file { + internal static string Selected_file_is_not_a_valid_file { get { - return ResourceManager.GetString("Selected_file_is_not_a_valid_executable_file", resourceCulture); + return ResourceManager.GetString("Selected_file_is_not_a_valid_file", resourceCulture); } } diff --git a/HeliosPlus/Resources/Language.resx b/HeliosPlus/Resources/Language.resx index 49688ff..b9fd69f 100644 --- a/HeliosPlus/Resources/Language.resx +++ b/HeliosPlus/Resources/Language.resx @@ -195,8 +195,8 @@ Helios Display Management - - Selected file is not a valid executable file. + + Selected file is not a valid file. Executable diff --git a/HeliosPlus/ShortcutRepository.cs b/HeliosPlus/ShortcutRepository.cs index e014e9f..1c0160a 100644 --- a/HeliosPlus/ShortcutRepository.cs +++ b/HeliosPlus/ShortcutRepository.cs @@ -597,17 +597,22 @@ namespace HeliosPlus // Look for the processes with the ProcessName we want (which in Windows is the filename without the extension) processesToMonitor = System.Diagnostics.Process.GetProcessesByName(Path.GetFileNameWithoutExtension(shortcutToUse.DifferentExecutableToMonitor)).ToList(); - // TODO: Fix this logic error that will only ever wait for the first process.... + // If we have found one or more processes then we should be good to go + // so let's break if (processesToMonitor.Count > 0) { break; } + // Let's wait a little while if we couldn't find + // any processes yet Thread.Sleep(300); ticks += 300; } - // If none started up before the timeout, then ignore them + // If we have reached the timeout and we cannot detect any + // of the alernative executables to monitor, then ignore that + // setting, and just monitor the process we just started instead if (processesToMonitor.Count == 0) { processesToMonitor.Add(process); @@ -622,20 +627,33 @@ namespace HeliosPlus if (substringStart < 0) substringStart = 0; notifyIcon.Text = $"HeliosPlus: Running {shortcutToUse.ExecutableNameAndPath.Substring(substringStart)}..."; - Application.DoEvents(); + //Application.DoEvents(); // Wait for the monitored process to exit - foreach (var p in processesToMonitor) + while (true) { - try + int processExitCount = 0; + // Check each process to see if it's exited + foreach (var p in processesToMonitor) { - p.WaitForExit(); - } - catch (Exception ex) - { - Console.WriteLine($"ShortcutRepository/RunShortcut exception 2: {ex.Message}: {ex.StackTrace} - {ex.InnerException}"); - // ignored + try + { + if (p.HasExited) + { + processExitCount++; + } + } + catch (InvalidOperationException ex) + { + Console.WriteLine($"ShortcutRepository/RunShortcut exception 2: {ex.Message}: {ex.StackTrace} - {ex.InnerException}"); + processExitCount++; + } } + + // Make sure that all processes have exited + // then start the shutdown process + if (processExitCount == processesToMonitor.Count) + break; } } diff --git a/HeliosPlus/UIForms/ShortcutForm.Designer.cs b/HeliosPlus/UIForms/ShortcutForm.Designer.cs index 707b944..c2ece68 100644 --- a/HeliosPlus/UIForms/ShortcutForm.Designer.cs +++ b/HeliosPlus/UIForms/ShortcutForm.Designer.cs @@ -42,6 +42,7 @@ namespace HeliosPlus.UIForms this.lbl_profile_shown_subtitle = new System.Windows.Forms.Label(); this.lbl_profile_shown = new System.Windows.Forms.Label(); this.ilv_saved_profiles = new Manina.Windows.Forms.ImageListView(); + this.dv_profile = new HeliosPlus.Shared.UserControls.DisplayView(); this.tabp_before = new System.Windows.Forms.TabPage(); this.pnl_start_program4 = new System.Windows.Forms.Panel(); this.cb_start_program4 = new System.Windows.Forms.CheckBox(); @@ -112,7 +113,6 @@ namespace HeliosPlus.UIForms this.lbl_title = new System.Windows.Forms.Label(); this.lbl_shortcut_name = new System.Windows.Forms.Label(); this.cb_autosuggest = new System.Windows.Forms.CheckBox(); - this.dv_profile = new HeliosPlus.Shared.UserControls.DisplayView(); this.tabc_shortcut.SuspendLayout(); this.tabp_display.SuspendLayout(); this.tabp_before.SuspendLayout(); @@ -169,7 +169,7 @@ namespace HeliosPlus.UIForms // this.dialog_open.DefaultExt = "exe"; this.dialog_open.FileName = "*.exe"; - this.dialog_open.Filter = global::HeliosPlus.Resources.Language.Executable_Files_Filter; + this.dialog_open.Filter = "All Files|*.*"; this.dialog_open.RestoreDirectory = true; this.dialog_open.SupportMultiDottedExtensions = true; // @@ -253,6 +253,21 @@ namespace HeliosPlus.UIForms this.ilv_saved_profiles.View = Manina.Windows.Forms.View.HorizontalStrip; this.ilv_saved_profiles.ItemClick += new Manina.Windows.Forms.ItemClickEventHandler(this.ilv_saved_profiles_ItemClick); // + // dv_profile + // + this.dv_profile.BackColor = System.Drawing.Color.DimGray; + this.dv_profile.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; + this.dv_profile.Font = new System.Drawing.Font("Consolas", 50F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); + this.dv_profile.ForeColor = System.Drawing.Color.MidnightBlue; + this.dv_profile.Location = new System.Drawing.Point(0, 0); + this.dv_profile.Margin = new System.Windows.Forms.Padding(18); + this.dv_profile.Name = "dv_profile"; + this.dv_profile.PaddingX = 100; + this.dv_profile.PaddingY = 100; + this.dv_profile.Profile = null; + this.dv_profile.Size = new System.Drawing.Size(1082, 467); + this.dv_profile.TabIndex = 23; + // // tabp_before // this.tabp_before.BackColor = System.Drawing.Color.Black; @@ -1070,21 +1085,6 @@ namespace HeliosPlus.UIForms this.cb_autosuggest.UseVisualStyleBackColor = true; this.cb_autosuggest.CheckedChanged += new System.EventHandler(this.cb_autosuggest_CheckedChanged); // - // dv_profile - // - this.dv_profile.BackColor = System.Drawing.Color.DimGray; - this.dv_profile.BorderStyle = System.Windows.Forms.BorderStyle.FixedSingle; - this.dv_profile.Font = new System.Drawing.Font("Consolas", 50F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0))); - this.dv_profile.ForeColor = System.Drawing.Color.MidnightBlue; - this.dv_profile.Location = new System.Drawing.Point(0, 0); - this.dv_profile.Margin = new System.Windows.Forms.Padding(18); - this.dv_profile.Name = "dv_profile"; - this.dv_profile.PaddingX = 100; - this.dv_profile.PaddingY = 100; - this.dv_profile.Profile = null; - this.dv_profile.Size = new System.Drawing.Size(1082, 467); - this.dv_profile.TabIndex = 23; - // // ShortcutForm // this.AcceptButton = this.btn_save; diff --git a/HeliosPlus/UIForms/ShortcutForm.cs b/HeliosPlus/UIForms/ShortcutForm.cs index 955b849..992dd0b 100644 --- a/HeliosPlus/UIForms/ShortcutForm.cs +++ b/HeliosPlus/UIForms/ShortcutForm.cs @@ -234,7 +234,7 @@ namespace HeliosPlus.UIForms else { MessageBox.Show( - Language.Selected_file_is_not_a_valid_executable_file, + Language.Selected_file_is_not_a_valid_file, Language.Executable, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); @@ -484,12 +484,12 @@ namespace HeliosPlus.UIForms if (rb_wait_alternative_executable.Checked && !String.IsNullOrWhiteSpace(txt_alternative_executable.Text)) { - _executableToUse.ProcessNameToMonitorUsesExecutable = true; + _executableToUse.ProcessNameToMonitorUsesExecutable = false; _executableToUse.DifferentExecutableToMonitor = txt_alternative_executable.Text; } else { - _executableToUse.ProcessNameToMonitorUsesExecutable = false; + _executableToUse.ProcessNameToMonitorUsesExecutable = true; } _shortcutToEdit = new ShortcutItem( @@ -522,6 +522,7 @@ namespace HeliosPlus.UIForms _executableToUse.ExecutableNameAndPath, _startPrograms, _autoName + ); /* _shortcutToEdit.UpdateNoGameShortcut( Name, @@ -976,7 +977,7 @@ namespace HeliosPlus.UIForms else { MessageBox.Show( - Language.Selected_file_is_not_a_valid_executable_file, + Language.Selected_file_is_not_a_valid_file, Language.Executable, MessageBoxButtons.OK, MessageBoxIcon.Exclamation); @@ -1236,7 +1237,7 @@ namespace HeliosPlus.UIForms string textToReturn = ""; if (dialog_open.ShowDialog(this) == DialogResult.OK) { - if (File.Exists(dialog_open.FileName) && Path.GetExtension(dialog_open.FileName) == @".exe") + if (File.Exists(dialog_open.FileName)) { textToReturn = dialog_open.FileName; dialog_open.FileName = string.Empty; @@ -1244,7 +1245,7 @@ namespace HeliosPlus.UIForms else { MessageBox.Show( - Language.Selected_file_is_not_a_valid_executable_file, + Language.Selected_file_is_not_a_valid_file, Language.Executable, MessageBoxButtons.OK, MessageBoxIcon.Exclamation);