Working waiting cancellation by toast

This commit is contained in:
Terry MacDonald 2021-12-03 10:59:52 +13:00
parent 20a7c85279
commit e3d85f9e96
3 changed files with 116 additions and 36 deletions

View File

@ -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;

View File

@ -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)]

View File

@ -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,6 +1175,21 @@ namespace DisplayMagician
}
}
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
@ -1177,6 +1198,8 @@ namespace DisplayMagician
.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,8 +1579,23 @@ namespace DisplayMagician
Thread.CurrentThread.Join(0);
Thread.Sleep(1000);
}
logger.Debug($"ShortcutRepository/RunShortcut: {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} has exited.");
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()
@ -1564,6 +1603,9 @@ namespace DisplayMagician
.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,6 +1670,22 @@ namespace DisplayMagician
// Pause for a second
Thread.Sleep(1000);
}
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
@ -1635,6 +1694,8 @@ namespace DisplayMagician
.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,8 +1817,23 @@ namespace DisplayMagician
Thread.CurrentThread.Join(0);
Thread.Sleep(1000);
}
logger.Debug($"ShortcutRepository/RunShortcut: {gameLibraryToUse.GameLibraryName} Game {gameToRun.Name} has exited.");
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()
@ -1763,6 +1841,9 @@ namespace DisplayMagician
.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();