From baa275b8a6d17736badb670699c896bf8d03a4fb Mon Sep 17 00:00:00 2001 From: Terry MacDonald Date: Wed, 17 Mar 2021 21:25:42 +1300 Subject: [PATCH] Option to start pgm only if needed Added the ability for user to only start a program if it hasn't been started yet. This is for things like SimHub that should already be started by Windows when the PC first boots. Also corrects a bug that ignored the autoname property so that it overwrote the suggested filename when saving a shortcut. --- DisplayMagician/ShortcutItem.cs | 8 +- DisplayMagician/ShortcutRepository.cs | 13 +++ .../UIForms/ShortcutForm.Designer.cs | 88 ++++++++++++++++--- DisplayMagician/UIForms/ShortcutForm.cs | 24 ++++- .../UIForms/ShortcutLibraryForm.cs | 25 ++++-- 5 files changed, 127 insertions(+), 31 deletions(-) diff --git a/DisplayMagician/ShortcutItem.cs b/DisplayMagician/ShortcutItem.cs index a9affbd..fb1f789 100644 --- a/DisplayMagician/ShortcutItem.cs +++ b/DisplayMagician/ShortcutItem.cs @@ -47,6 +47,7 @@ namespace DisplayMagician public string Arguments; public bool ExecutableArgumentsRequired; public bool CloseOnFinish; + public bool DontStartIfAlreadyRunning; } public struct Executable @@ -2237,12 +2238,7 @@ namespace DisplayMagician if (System.IO.File.Exists(shortcutFileName)) { System.IO.File.Delete(shortcutFileName); - } - - // Actually create the shortcut! - //var wshShellType = Type.GetTypeFromCLSID(new Guid("72C24DD5-D70A-438B-8A42-98424B88AFB8")); - //dynamic wshShell = Activator.CreateInstance(wshShellType); - + } WshShell shell = new WshShell(); IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutFileName); diff --git a/DisplayMagician/ShortcutRepository.cs b/DisplayMagician/ShortcutRepository.cs index 7e61779..720cda2 100644 --- a/DisplayMagician/ShortcutRepository.cs +++ b/DisplayMagician/ShortcutRepository.cs @@ -708,6 +708,19 @@ namespace DisplayMagician logger.Info($"ShortcutRepository/RunShortcut: Starting {startProgramsToStart.Count} programs before the main game or executable"); foreach (StartProgram processToStart in startProgramsToStart) { + // If required, check whether a process is started already + if (processToStart.DontStartIfAlreadyRunning) + { + logger.Info($"ShortcutRepository/RunShortcut: Checking if process {processToStart.Executable} is already running"); + Process[] alreadyRunningProcesses = System.Diagnostics.Process.GetProcessesByName(Path.GetFileNameWithoutExtension(processToStart.Executable)); + if (alreadyRunningProcesses.Length > 0) + { + logger.Info($"ShortcutRepository/RunShortcut: Process {processToStart.Executable} is already running, so we won't start another one"); + break; + } + + } + // Start the executable logger.Info($"ShortcutRepository/RunShortcut: Starting process {processToStart.Executable}"); Process process = null; diff --git a/DisplayMagician/UIForms/ShortcutForm.Designer.cs b/DisplayMagician/UIForms/ShortcutForm.Designer.cs index 204bf35..f352d99 100644 --- a/DisplayMagician/UIForms/ShortcutForm.Designer.cs +++ b/DisplayMagician/UIForms/ShortcutForm.Designer.cs @@ -142,6 +142,10 @@ namespace DisplayMagician.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.cb_dont_start_if_running1 = new System.Windows.Forms.CheckBox(); + this.cb_dont_start_if_running2 = new System.Windows.Forms.CheckBox(); + this.cb_dont_start_if_running3 = new System.Windows.Forms.CheckBox(); + this.cb_dont_start_if_running4 = new System.Windows.Forms.CheckBox(); this.tabc_shortcut.SuspendLayout(); this.tabp_display.SuspendLayout(); this.tabp_audio.SuspendLayout(); @@ -606,6 +610,7 @@ namespace DisplayMagician.UIForms // // pnl_start_program4 // + this.pnl_start_program4.Controls.Add(this.cb_dont_start_if_running4); this.pnl_start_program4.Controls.Add(this.cb_start_program4); this.pnl_start_program4.Controls.Add(this.txt_start_program4); this.pnl_start_program4.Controls.Add(this.cb_start_program_close4); @@ -639,11 +644,11 @@ namespace DisplayMagician.UIForms // this.cb_start_program_close4.AutoSize = true; this.cb_start_program_close4.ForeColor = System.Drawing.Color.White; - this.cb_start_program_close4.Location = new System.Drawing.Point(167, 87); + this.cb_start_program_close4.Location = new System.Drawing.Point(547, 82); this.cb_start_program_close4.Name = "cb_start_program_close4"; - this.cb_start_program_close4.Size = new System.Drawing.Size(344, 24); + this.cb_start_program_close4.Size = new System.Drawing.Size(398, 24); this.cb_start_program_close4.TabIndex = 16; - this.cb_start_program_close4.Text = "Close program when you finish playing Game"; + this.cb_start_program_close4.Text = "Close started program when you finish playing Game"; this.cb_start_program_close4.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.cb_start_program_close4.UseVisualStyleBackColor = true; this.cb_start_program_close4.Visible = false; @@ -695,6 +700,7 @@ namespace DisplayMagician.UIForms // // pnl_start_program3 // + this.pnl_start_program3.Controls.Add(this.cb_dont_start_if_running3); this.pnl_start_program3.Controls.Add(this.cb_start_program3); this.pnl_start_program3.Controls.Add(this.txt_start_program3); this.pnl_start_program3.Controls.Add(this.cb_start_program_close3); @@ -728,11 +734,11 @@ namespace DisplayMagician.UIForms // this.cb_start_program_close3.AutoSize = true; this.cb_start_program_close3.ForeColor = System.Drawing.Color.White; - this.cb_start_program_close3.Location = new System.Drawing.Point(167, 87); + this.cb_start_program_close3.Location = new System.Drawing.Point(547, 82); this.cb_start_program_close3.Name = "cb_start_program_close3"; - this.cb_start_program_close3.Size = new System.Drawing.Size(344, 24); + this.cb_start_program_close3.Size = new System.Drawing.Size(398, 24); this.cb_start_program_close3.TabIndex = 16; - this.cb_start_program_close3.Text = "Close program when you finish playing Game"; + this.cb_start_program_close3.Text = "Close started program when you finish playing Game"; this.cb_start_program_close3.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.cb_start_program_close3.UseVisualStyleBackColor = true; this.cb_start_program_close3.Visible = false; @@ -784,6 +790,7 @@ namespace DisplayMagician.UIForms // // pnl_start_program2 // + this.pnl_start_program2.Controls.Add(this.cb_dont_start_if_running2); this.pnl_start_program2.Controls.Add(this.cb_start_program2); this.pnl_start_program2.Controls.Add(this.txt_start_program2); this.pnl_start_program2.Controls.Add(this.cb_start_program_close2); @@ -817,11 +824,11 @@ namespace DisplayMagician.UIForms // this.cb_start_program_close2.AutoSize = true; this.cb_start_program_close2.ForeColor = System.Drawing.Color.White; - this.cb_start_program_close2.Location = new System.Drawing.Point(167, 87); + this.cb_start_program_close2.Location = new System.Drawing.Point(547, 82); this.cb_start_program_close2.Name = "cb_start_program_close2"; - this.cb_start_program_close2.Size = new System.Drawing.Size(344, 24); + this.cb_start_program_close2.Size = new System.Drawing.Size(398, 24); this.cb_start_program_close2.TabIndex = 16; - this.cb_start_program_close2.Text = "Close program when you finish playing Game"; + this.cb_start_program_close2.Text = "Close started program when you finish playing Game"; this.cb_start_program_close2.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.cb_start_program_close2.UseVisualStyleBackColor = true; this.cb_start_program_close2.Visible = false; @@ -873,6 +880,7 @@ namespace DisplayMagician.UIForms // // pnl_start_program1 // + this.pnl_start_program1.Controls.Add(this.cb_dont_start_if_running1); this.pnl_start_program1.Controls.Add(this.cb_start_program1); this.pnl_start_program1.Controls.Add(this.txt_start_program1); this.pnl_start_program1.Controls.Add(this.cb_start_program_close1); @@ -906,11 +914,11 @@ namespace DisplayMagician.UIForms // this.cb_start_program_close1.AutoSize = true; this.cb_start_program_close1.ForeColor = System.Drawing.Color.White; - this.cb_start_program_close1.Location = new System.Drawing.Point(167, 87); + this.cb_start_program_close1.Location = new System.Drawing.Point(547, 82); this.cb_start_program_close1.Name = "cb_start_program_close1"; - this.cb_start_program_close1.Size = new System.Drawing.Size(344, 24); + this.cb_start_program_close1.Size = new System.Drawing.Size(398, 24); this.cb_start_program_close1.TabIndex = 16; - this.cb_start_program_close1.Text = "Close program when you finish playing Game"; + this.cb_start_program_close1.Text = "Close started program when you finish playing Game"; this.cb_start_program_close1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; this.cb_start_program_close1.UseVisualStyleBackColor = true; this.cb_start_program_close1.Visible = false; @@ -1517,6 +1525,58 @@ namespace DisplayMagician.UIForms this.cb_autosuggest.UseVisualStyleBackColor = true; this.cb_autosuggest.CheckedChanged += new System.EventHandler(this.cb_autosuggest_CheckedChanged); // + // cb_dont_start_if_running1 + // + this.cb_dont_start_if_running1.AutoSize = true; + this.cb_dont_start_if_running1.ForeColor = System.Drawing.Color.White; + this.cb_dont_start_if_running1.Location = new System.Drawing.Point(167, 82); + this.cb_dont_start_if_running1.Name = "cb_dont_start_if_running1"; + this.cb_dont_start_if_running1.Size = new System.Drawing.Size(289, 24); + this.cb_dont_start_if_running1.TabIndex = 18; + this.cb_dont_start_if_running1.Text = "Don\'t start if program already running"; + this.cb_dont_start_if_running1.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + this.cb_dont_start_if_running1.UseVisualStyleBackColor = true; + this.cb_dont_start_if_running1.Visible = false; + // + // cb_dont_start_if_running2 + // + this.cb_dont_start_if_running2.AutoSize = true; + this.cb_dont_start_if_running2.ForeColor = System.Drawing.Color.White; + this.cb_dont_start_if_running2.Location = new System.Drawing.Point(167, 82); + this.cb_dont_start_if_running2.Name = "cb_dont_start_if_running2"; + this.cb_dont_start_if_running2.Size = new System.Drawing.Size(289, 24); + this.cb_dont_start_if_running2.TabIndex = 19; + this.cb_dont_start_if_running2.Text = "Don\'t start if program already running"; + this.cb_dont_start_if_running2.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + this.cb_dont_start_if_running2.UseVisualStyleBackColor = true; + this.cb_dont_start_if_running2.Visible = false; + // + // cb_dont_start_if_running3 + // + this.cb_dont_start_if_running3.AutoSize = true; + this.cb_dont_start_if_running3.ForeColor = System.Drawing.Color.White; + this.cb_dont_start_if_running3.Location = new System.Drawing.Point(167, 82); + this.cb_dont_start_if_running3.Name = "cb_dont_start_if_running3"; + this.cb_dont_start_if_running3.Size = new System.Drawing.Size(289, 24); + this.cb_dont_start_if_running3.TabIndex = 20; + this.cb_dont_start_if_running3.Text = "Don\'t start if program already running"; + this.cb_dont_start_if_running3.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + this.cb_dont_start_if_running3.UseVisualStyleBackColor = true; + this.cb_dont_start_if_running3.Visible = false; + // + // cb_dont_start_if_running4 + // + this.cb_dont_start_if_running4.AutoSize = true; + this.cb_dont_start_if_running4.ForeColor = System.Drawing.Color.White; + this.cb_dont_start_if_running4.Location = new System.Drawing.Point(167, 82); + this.cb_dont_start_if_running4.Name = "cb_dont_start_if_running4"; + this.cb_dont_start_if_running4.Size = new System.Drawing.Size(289, 24); + this.cb_dont_start_if_running4.TabIndex = 20; + this.cb_dont_start_if_running4.Text = "Don\'t start if program already running"; + this.cb_dont_start_if_running4.TextAlign = System.Drawing.ContentAlignment.MiddleRight; + this.cb_dont_start_if_running4.UseVisualStyleBackColor = true; + this.cb_dont_start_if_running4.Visible = false; + // // ShortcutForm // this.AcceptButton = this.btn_save; @@ -1695,5 +1755,9 @@ namespace DisplayMagician.UIForms private System.Windows.Forms.RadioButton rb_switch_capture_permanent; private System.Windows.Forms.RadioButton rb_switch_capture_temp; private System.Windows.Forms.Label lbl_no_game_libraries; + private System.Windows.Forms.CheckBox cb_dont_start_if_running4; + private System.Windows.Forms.CheckBox cb_dont_start_if_running3; + private System.Windows.Forms.CheckBox cb_dont_start_if_running2; + private System.Windows.Forms.CheckBox cb_dont_start_if_running1; } } \ No newline at end of file diff --git a/DisplayMagician/UIForms/ShortcutForm.cs b/DisplayMagician/UIForms/ShortcutForm.cs index d985e4c..2bf6690 100644 --- a/DisplayMagician/UIForms/ShortcutForm.cs +++ b/DisplayMagician/UIForms/ShortcutForm.cs @@ -361,7 +361,8 @@ namespace DisplayMagician.UIForms Executable = txt_start_program1.Text, ExecutableArgumentsRequired = cb_start_program_pass_args1.Checked, Arguments = txt_start_program_args1.Text, - CloseOnFinish = cb_start_program_close1.Checked + CloseOnFinish = cb_start_program_close1.Checked, + DontStartIfAlreadyRunning = cb_dont_start_if_running1.Checked }; _startPrograms.Add(myStartProgram); @@ -372,7 +373,8 @@ namespace DisplayMagician.UIForms Enabled = cb_start_program2.Checked, ExecutableArgumentsRequired = cb_start_program_pass_args2.Checked, Arguments = txt_start_program_args2.Text, - CloseOnFinish = cb_start_program_close2.Checked + CloseOnFinish = cb_start_program_close2.Checked, + DontStartIfAlreadyRunning = cb_dont_start_if_running2.Checked }; _startPrograms.Add(myStartProgram); @@ -383,7 +385,8 @@ namespace DisplayMagician.UIForms Enabled = cb_start_program3.Checked, ExecutableArgumentsRequired = cb_start_program_pass_args3.Checked, Arguments = txt_start_program_args3.Text, - CloseOnFinish = cb_start_program_close3.Checked + CloseOnFinish = cb_start_program_close3.Checked, + DontStartIfAlreadyRunning = cb_dont_start_if_running3.Checked }; _startPrograms.Add(myStartProgram); @@ -394,7 +397,8 @@ namespace DisplayMagician.UIForms Enabled = cb_start_program4.Checked, ExecutableArgumentsRequired = cb_start_program_pass_args4.Checked, Arguments = txt_start_program_args4.Text, - CloseOnFinish = cb_start_program_close4.Checked + CloseOnFinish = cb_start_program_close4.Checked, + DontStartIfAlreadyRunning = cb_dont_start_if_running4.Checked }; _startPrograms.Add(myStartProgram); @@ -1007,6 +1011,7 @@ namespace DisplayMagician.UIForms cb_start_program_pass_args1.Checked = myStartProgram.ExecutableArgumentsRequired; txt_start_program_args1.Text = myStartProgram.Arguments; cb_start_program_close1.Checked = myStartProgram.CloseOnFinish; + cb_dont_start_if_running1.Checked = myStartProgram.DontStartIfAlreadyRunning; break; case 2: txt_start_program2.Text = myStartProgram.Executable; @@ -1014,6 +1019,7 @@ namespace DisplayMagician.UIForms cb_start_program_pass_args2.Checked = myStartProgram.ExecutableArgumentsRequired; txt_start_program_args2.Text = myStartProgram.Arguments; cb_start_program_close2.Checked = myStartProgram.CloseOnFinish; + cb_dont_start_if_running2.Checked = myStartProgram.DontStartIfAlreadyRunning; break; case 3: txt_start_program3.Text = myStartProgram.Executable; @@ -1021,6 +1027,7 @@ namespace DisplayMagician.UIForms cb_start_program_pass_args3.Checked = myStartProgram.ExecutableArgumentsRequired; txt_start_program_args3.Text = myStartProgram.Arguments; cb_start_program_close3.Checked = myStartProgram.CloseOnFinish; + cb_dont_start_if_running3.Checked = myStartProgram.DontStartIfAlreadyRunning; break; case 4: txt_start_program4.Text = myStartProgram.Executable; @@ -1028,6 +1035,7 @@ namespace DisplayMagician.UIForms cb_start_program_pass_args4.Checked = myStartProgram.ExecutableArgumentsRequired; txt_start_program_args4.Text = myStartProgram.Arguments; cb_start_program_close4.Checked = myStartProgram.CloseOnFinish; + cb_dont_start_if_running4.Checked = myStartProgram.DontStartIfAlreadyRunning; break; } @@ -1479,6 +1487,7 @@ namespace DisplayMagician.UIForms btn_start_program1.Visible = true; cb_start_program_pass_args1.Visible = true; cb_start_program_close1.Visible = true; + cb_dont_start_if_running1.Visible = true; } else { @@ -1487,6 +1496,7 @@ namespace DisplayMagician.UIForms btn_start_program1.Visible = false; cb_start_program_pass_args1.Visible = false; cb_start_program_close1.Visible = false; + cb_dont_start_if_running1.Visible = false; } } @@ -1502,6 +1512,7 @@ namespace DisplayMagician.UIForms btn_start_program2.Visible = true; cb_start_program_pass_args2.Visible = true; cb_start_program_close2.Visible = true; + cb_dont_start_if_running2.Visible = true; } else { @@ -1510,6 +1521,7 @@ namespace DisplayMagician.UIForms btn_start_program2.Visible = false; cb_start_program_pass_args2.Visible = false; cb_start_program_close2.Visible = false; + cb_dont_start_if_running2.Visible = false; } } @@ -1525,6 +1537,7 @@ namespace DisplayMagician.UIForms btn_start_program3.Visible = true; cb_start_program_pass_args3.Visible = true; cb_start_program_close3.Visible = true; + cb_dont_start_if_running3.Visible = true; } else { @@ -1533,6 +1546,7 @@ namespace DisplayMagician.UIForms btn_start_program3.Visible = false; cb_start_program_pass_args3.Visible = false; cb_start_program_close3.Visible = false; + cb_dont_start_if_running3.Visible = false; } } @@ -1548,6 +1562,7 @@ namespace DisplayMagician.UIForms btn_start_program4.Visible = true; cb_start_program_pass_args4.Visible = true; cb_start_program_close4.Visible = true; + cb_dont_start_if_running4.Visible = true; } else { @@ -1556,6 +1571,7 @@ namespace DisplayMagician.UIForms btn_start_program4.Visible = false; cb_start_program_pass_args4.Visible = false; cb_start_program_close4.Visible = false; + cb_dont_start_if_running4.Visible = false; } } diff --git a/DisplayMagician/UIForms/ShortcutLibraryForm.cs b/DisplayMagician/UIForms/ShortcutLibraryForm.cs index 1b32df9..e9f8969 100644 --- a/DisplayMagician/UIForms/ShortcutLibraryForm.cs +++ b/DisplayMagician/UIForms/ShortcutLibraryForm.cs @@ -122,22 +122,29 @@ namespace DisplayMagician.UIForms // Set the Shortcut save folder to the Desktop as that's where people will want it most likely dialog_save.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // Try to set up some sensible suggestions for the Shortcut name - if (_selectedShortcut.DisplayPermanence == ShortcutPermanence.Permanent) + if (_selectedShortcut.AutoName) { - - dialog_save.FileName = _selectedShortcut.Name; - } - else - { - if (_selectedShortcut.Category == ShortcutCategory.Application) + if (_selectedShortcut.DisplayPermanence == ShortcutPermanence.Permanent) { - dialog_save.FileName = String.Concat(Path.GetFileNameWithoutExtension(_selectedShortcut.ExecutableNameAndPath), @" (", _selectedShortcut.Name.ToLower(CultureInfo.InvariantCulture), @")"); + + dialog_save.FileName = _selectedShortcut.Name; } else { - dialog_save.FileName = _selectedShortcut.Name; + if (_selectedShortcut.Category == ShortcutCategory.Application) + { + dialog_save.FileName = String.Concat(Path.GetFileNameWithoutExtension(_selectedShortcut.ExecutableNameAndPath), @" (", _selectedShortcut.Name.ToLower(CultureInfo.InvariantCulture), @")"); + } + else + { + dialog_save.FileName = _selectedShortcut.Name; + } } } + else + { + dialog_save.FileName = _selectedShortcut.Name; + } // Show the Save Shortcut window if (dialog_save.ShowDialog(this) == DialogResult.OK)