diff --git a/DisplayMagician/DesktopNotificationActivator.cs b/DisplayMagician/DesktopNotificationActivator.cs index d09cafb..1587021 100644 --- a/DisplayMagician/DesktopNotificationActivator.cs +++ b/DisplayMagician/DesktopNotificationActivator.cs @@ -30,7 +30,7 @@ namespace DisplayMagician if (myArg.Name.Equals("action",StringComparison.OrdinalIgnoreCase)) { // See what action is being requested - switch (args["action"].ToLowerInvariant()) + switch (args["action"]) { // Open the Main window case "open": @@ -49,7 +49,6 @@ namespace DisplayMagician // Stop waiting so that the monitoring stops, and the UI becomes free case "stopWaiting": - MessageBox.Show("User just asked DisplayMagician to stop monitoring the game"); ShortcutRepository.CancelWait = true; break; diff --git a/DisplayMagician/Properties/AssemblyInfo.cs b/DisplayMagician/Properties/AssemblyInfo.cs index f982e7e..6180bcf 100644 --- a/DisplayMagician/Properties/AssemblyInfo.cs +++ b/DisplayMagician/Properties/AssemblyInfo.cs @@ -26,8 +26,8 @@ using System.Resources; [assembly: Guid("e4ceaf5e-ad01-4695-b179-31168eb74c48")] // Version information -[assembly: AssemblyVersion("2.1.1.19")] -[assembly: AssemblyFileVersion("2.1.1.19")] +[assembly: AssemblyVersion("2.1.1.22")] +[assembly: AssemblyFileVersion("2.1.1.22")] [assembly: NeutralResourcesLanguageAttribute( "en" )] [assembly: CLSCompliant(true)] diff --git a/DisplayMagician/ShortcutRepository.cs b/DisplayMagician/ShortcutRepository.cs index a4fdbee..38094b0 100644 --- a/DisplayMagician/ShortcutRepository.cs +++ b/DisplayMagician/ShortcutRepository.cs @@ -1155,6 +1155,7 @@ namespace DisplayMagician { while (true) { + Application.DoEvents(); // If we have no more processes left then we're done! if (ProcessUtils.ProcessExited(processesToMonitor)) { @@ -1162,6 +1163,11 @@ namespace DisplayMagician break; } + if (_cancelWait) + { + logger.Debug($"ShortcutRepository/RunShortcut: User requested we stop waiting. Exiting loop while waiting for application {shortcutToUse.ExecutableNameAndPath} 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); @@ -1169,14 +1175,31 @@ namespace DisplayMagician } } - logger.Debug($"ShortcutRepository/RunShortcut: Creating a Windows Toast to notify the user that the executable {shortcutToUse.ExecutableNameAndPath} has closed."); - // Tell the user that the application has closed - // Construct the toast content - tcBuilder = new ToastContentBuilder() - .AddToastActivationInfo("notify=stopApplicationDetected", ToastActivationType.Foreground) - .AddText($"{shortcutToUse.ExecutableNameAndPath} was closed", hintMaxLines: 1) - .AddText($"All {processToMonitorName} processes were shutdown and changes were reverted.") - .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + if (_cancelWait) + { + // The monitoring was stopped by the user + logger.Debug($"ShortcutRepository/RunShortcut: Creating a Windows Toast to notify the user that the executable {shortcutToUse.ExecutableNameAndPath} monitoring was stopped by the user."); + // Construct the toast content + tcBuilder = new ToastContentBuilder() + .AddToastActivationInfo("notify=stopApplicationByUser", ToastActivationType.Foreground) + .AddText($"{shortcutToUse.ExecutableNameAndPath} monitoring cancelled", hintMaxLines: 1) + .AddText($"Monitoring of {processToMonitorName} processes were stopped by the user.") + .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + + } + else + { + // The program was closed normally + logger.Debug($"ShortcutRepository/RunShortcut: Creating a Windows Toast to notify the user that the executable {shortcutToUse.ExecutableNameAndPath} has closed."); + // Tell the user that the application has closed + // Construct the toast content + tcBuilder = new ToastContentBuilder() + .AddToastActivationInfo("notify=stopApplicationDetected", ToastActivationType.Foreground) + .AddText($"{shortcutToUse.ExecutableNameAndPath} was closed", hintMaxLines: 1) + .AddText($"All {processToMonitorName} processes were shutdown and changes were reverted.") + .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + + } toastContent = tcBuilder.Content; // Make sure to use Windows.Data.Xml.Dom doc = new XmlDocument(); @@ -1538,6 +1561,7 @@ namespace DisplayMagician logger.Debug($"ShortcutRepository/RunShortcut: waiting for {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} to exit."); while (true) { + Application.DoEvents(); if (!gameToRun.IsRunning) { logger.Debug($"ShortcutRepository/RunShortcut: {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} is no longer running (IsRunning is false)."); @@ -1555,15 +1579,33 @@ namespace DisplayMagician Thread.CurrentThread.Join(0); Thread.Sleep(1000); } - logger.Debug($"ShortcutRepository/RunShortcut: {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} has exited."); - // Tell the user that the Game has closed - // Construct the toast content - tcBuilder = new ToastContentBuilder() - .AddToastActivationInfo("notify=stopGameDetected", ToastActivationType.Foreground) - .AddText($"{shortcutToUse.GameName} was closed", hintMaxLines: 1) - .AddText($"{shortcutToUse.GameName} game was exited.") - .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + if (_cancelWait) + { + // The monitoring was stopped by the user + logger.Debug($"ShortcutRepository/RunShortcut: Creating a Windows Toast to notify the user that the {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} monitoring was stopped by the user."); + // Construct the toast content + tcBuilder = new ToastContentBuilder() + .AddToastActivationInfo("notify=stopGameByUser", ToastActivationType.Foreground) + .AddText($"{gameToRun.Name} Game monitoring cancelled", hintMaxLines: 1) + .AddText($"Monitoring of {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} was stopped by the user.") + .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + + } + else + { + // The program was closed normally + logger.Debug($"ShortcutRepository/RunShortcut: {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} has exited."); + // Tell the user that the Game has closed + // Construct the toast content + tcBuilder = new ToastContentBuilder() + .AddToastActivationInfo("notify=stopGameDetected", ToastActivationType.Foreground) + .AddText($"{shortcutToUse.GameName} was closed", hintMaxLines: 1) + .AddText($"{shortcutToUse.GameName} game was exited.") + .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + + } + toastContent = tcBuilder.Content; // Make sure to use Windows.Data.Xml.Dom doc = new XmlDocument(); @@ -1605,6 +1647,7 @@ namespace DisplayMagician while (true) { + Application.DoEvents(); processesToMonitor = Process.GetProcessesByName(altGameProcessToMonitor).ToList(); // If we have no more processes left then we're done! @@ -1627,14 +1670,32 @@ namespace DisplayMagician // Pause for a second Thread.Sleep(1000); } - logger.Debug($"ShortcutRepository/RunShortcut: Alternative Game Executable {altGameProcessToMonitor} has exited."); - // Tell the user that the Alt Game Executable has closed - // Construct the toast content - tcBuilder = new ToastContentBuilder() - .AddToastActivationInfo("notify=stopGameDetected", ToastActivationType.Foreground) - .AddText($"{altGameProcessToMonitor} was closed", hintMaxLines: 1) - .AddText($"{altGameProcessToMonitor} alternative game executable was exited.") - .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + + if (_cancelWait) + { + // The monitoring was stopped by the user + logger.Debug($"ShortcutRepository/RunShortcut: Creating a Windows Toast to notify the user that the Alternative Game Executable {altGameProcessToMonitor} monitoring was stopped by the user."); + // Construct the toast content + tcBuilder = new ToastContentBuilder() + .AddToastActivationInfo("notify=stopGameByUser", ToastActivationType.Foreground) + .AddText($"{altGameProcessToMonitor} monitoring cancelled", hintMaxLines: 1) + .AddText($"Monitoring of {altGameProcessToMonitor} alternative game executable was stopped by the user.") + .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + + } + else + { + // The program was closed normally + logger.Debug($"ShortcutRepository/RunShortcut: Alternative Game Executable {altGameProcessToMonitor} has exited."); + // Tell the user that the Alt Game Executable has closed + // Construct the toast content + tcBuilder = new ToastContentBuilder() + .AddToastActivationInfo("notify=stopGameDetected", ToastActivationType.Foreground) + .AddText($"{altGameProcessToMonitor} was closed", hintMaxLines: 1) + .AddText($"{altGameProcessToMonitor} alternative game executable was exited.") + .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + } + toastContent = tcBuilder.Content; // Make sure to use Windows.Data.Xml.Dom doc = new XmlDocument(); @@ -1681,6 +1742,7 @@ namespace DisplayMagician if (gameToRun.IsRunning) { // The game is running! So now we continue processing + Application.DoEvents(); gameRunning = true; logger.Debug($"ShortcutRepository/RunShortcut: Found the '{gameToRun.Name}' process has started"); @@ -1737,6 +1799,7 @@ namespace DisplayMagician logger.Debug($"ShortcutRepository/RunShortcut: waiting for {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} to exit."); while (true) { + Application.DoEvents(); if (!gameToRun.IsRunning) { logger.Debug($"ShortcutRepository/RunShortcut: {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} is no longer running (IsRunning is false)."); @@ -1754,15 +1817,33 @@ namespace DisplayMagician Thread.CurrentThread.Join(0); Thread.Sleep(1000); } - logger.Debug($"ShortcutRepository/RunShortcut: {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} has exited."); - // Tell the user that the Game has closed - // Construct the toast content - tcBuilder = new ToastContentBuilder() - .AddToastActivationInfo("notify=stopGameDetected", ToastActivationType.Foreground) - .AddText($"{shortcutToUse.GameName} was closed", hintMaxLines: 1) - .AddText($"{shortcutToUse.GameName} game was exited.") - .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + if (_cancelWait) + { + // The monitoring was stopped by the user + logger.Debug($"ShortcutRepository/RunShortcut: Creating a Windows Toast to notify the user that the {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} monitoring was stopped by the user."); + // Construct the toast content + tcBuilder = new ToastContentBuilder() + .AddToastActivationInfo("notify=stopGameByUser", ToastActivationType.Foreground) + .AddText($"{gameToRun.Name} Game monitoring cancelled", hintMaxLines: 1) + .AddText($"Monitoring of {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} was stopped by the user.") + .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + + } + else + { + // The program was closed normally + logger.Debug($"ShortcutRepository/RunShortcut: {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} has exited."); + // Tell the user that the Game has closed + // Construct the toast content + tcBuilder = new ToastContentBuilder() + .AddToastActivationInfo("notify=stopGameDetected", ToastActivationType.Foreground) + .AddText($"{shortcutToUse.GameName} was closed", hintMaxLines: 1) + .AddText($"{shortcutToUse.GameName} game was exited.") + .AddAudio(new Uri("ms-winsoundevent:Notification.Default"), false, true); + } + + toastContent = tcBuilder.Content; // Make sure to use Windows.Data.Xml.Dom doc = new XmlDocument();