Correctly close programs that we start

The StartPrograms processes now all correctly are added
to a list of processes to stop only if the user requested they
are stopped.

Additionally, a 300ms delay was added after SteamGame is
launched to give it time to actually run the darn game! It was
erroring due to testing for the launch too quickly. Steam
takes a wee bit of time to start the game, and we need to
account for that.
This commit is contained in:
Terry MacDonald 2020-10-13 22:02:46 +13:00
parent 86776f1aba
commit 3992157793

View File

@ -7,6 +7,7 @@ using NvAPIWrapper.Mosaic;
using NvAPIWrapper.Native.Mosaic;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Drawing.IconLib;
using System.IO;
@ -518,6 +519,7 @@ namespace HeliosPlus
else
process = System.Diagnostics.Process.Start(processToStart.Executable);
// Record t
if (processToStart.CloseOnFinish)
startProgramsToStop.Add(process);
}
}
@ -635,7 +637,7 @@ namespace HeliosPlus
}
// Start the URI Handler to run Steam
var steamProcess = System.Diagnostics.Process.Start(address);
var steamProcess = Process.Start(address);
// Wait for Steam game to update if needed
var ticks = 0;
@ -678,12 +680,13 @@ namespace HeliosPlus
// ignored
}*/
// Wait for the game to exit
// Wait 300ms for the game process to spawn
Thread.Sleep(300);
// Now check it's actually started
if (steamGameToRun.IsRunning)
{
// Wait for the game to exit
while (true)
{
if (!steamGameToRun.IsRunning)
@ -729,9 +732,24 @@ namespace HeliosPlus
foreach (Process processToStop in startProgramsToStop.Reverse<Process>())
{
Console.WriteLine($"Stopping process {processToStop.StartInfo.FileName}");
try
{
// Stop the program
processToStop.CloseMainWindow();
processToStop.WaitForExit(5000);
if (!processToStop.HasExited)
{
Console.WriteLine($"- Process {processToStop.StartInfo.FileName} wouldn't stop cleanly. Forcing program close.");
processToStop.Kill();
processToStop.WaitForExit(5000);
}
processToStop.Close();
}
catch (Win32Exception ex) { }
catch (InvalidOperationException ex) { }
catch (AggregateException ae) { }
}
}
// Change back to the original profile only if it is different