diff --git a/StarterNG.Tests/StartSimulationTests.cs b/StarterNG.Tests/StartSimulationTests.cs index 81d3ece..359c73c 100644 --- a/StarterNG.Tests/StartSimulationTests.cs +++ b/StarterNG.Tests/StartSimulationTests.cs @@ -401,8 +401,6 @@ public class StartSimulationTests public bool OpenInShell(string pathOrUrl) => false; public IProcessHandle? FindRunning(string processName) => null; - - public bool IsRunning(string processName) => false; } private sealed class RunningProcess : IProcessHandle diff --git a/StarterNG/Application/Abstractions/IProcessLauncher.cs b/StarterNG/Application/Abstractions/IProcessLauncher.cs index f93d56b..9131240 100644 --- a/StarterNG/Application/Abstractions/IProcessLauncher.cs +++ b/StarterNG/Application/Abstractions/IProcessLauncher.cs @@ -31,6 +31,4 @@ public interface IProcessLauncher bool OpenInShell(string pathOrUrl); IProcessHandle? FindRunning(string processName); - - bool IsRunning(string processName); } diff --git a/StarterNG/Application/AppServices.cs b/StarterNG/Application/AppServices.cs index ec064d9..7f7b6ce 100644 --- a/StarterNG/Application/AppServices.cs +++ b/StarterNG/Application/AppServices.cs @@ -65,8 +65,7 @@ public sealed class AppServices SettingsStore = new SettingsStore(files, clock, log, settingsPaths, new SettingsSerializer(dlssPlatform: !environment.IsMacOS), Executables); MissingVehicleLog = new MissingVehicleLog(SettingsStore.Settings, missingAssets, Library, log); Favorites = new FavoriteSceneries(files, log, settingsPaths.FavoritesPath()); - Tapes = new HaslerTapes(files, paths, log, clock, Timetables, - () => Processes.IsRunning(Path.GetFileNameWithoutExtension(SettingsStore.ResolveExecutable()))); + Tapes = new HaslerTapes(files, paths, log, clock, Timetables, () => State.SimulatorRunning); Gpus = new LinuxGpus(files, environment); GraphicsCards = new GraphicsCards(files, environment, Gpus); Displays = new Displays(files, environment); diff --git a/StarterNG/Application/AppState.cs b/StarterNG/Application/AppState.cs index 78ab716..bd5cbba 100644 --- a/StarterNG/Application/AppState.cs +++ b/StarterNG/Application/AppState.cs @@ -46,5 +46,17 @@ public sealed class AppState } } + private volatile bool _simulatorRunning; + + /// + /// The simulator the starter watches is still running: the one it started, not + /// any of the same name. Read off the UI thread too. + /// + public bool SimulatorRunning + { + get => _simulatorRunning; + set => _simulatorRunning = value; + } + public void NotifyChanged() => Changed?.Invoke(); } diff --git a/StarterNG/Infrastructure/Adapters/SystemProcessLauncher.cs b/StarterNG/Infrastructure/Adapters/SystemProcessLauncher.cs index 45cf7b3..99f06df 100644 --- a/StarterNG/Infrastructure/Adapters/SystemProcessLauncher.cs +++ b/StarterNG/Infrastructure/Adapters/SystemProcessLauncher.cs @@ -90,22 +90,6 @@ public sealed class SystemProcessLauncher : IProcessLauncher } } - public bool IsRunning(string processName) - { - try - { - var found = Process.GetProcessesByName(processName); - foreach (var process in found) - process.Dispose(); - return found.Length > 0; - } - catch (Exception ex) - { - _log.Log($"lookup {processName}", ex); - return false; - } - } - private sealed class ProcessHandle : IProcessHandle { private const int KeptErrorLines = 20; diff --git a/StarterNG/MainWindow.axaml.cs b/StarterNG/MainWindow.axaml.cs index 0ab7b58..8d71cd0 100644 --- a/StarterNG/MainWindow.axaml.cs +++ b/StarterNG/MainWindow.axaml.cs @@ -39,7 +39,6 @@ public partial class MainWindow : Window private DateTime _openedUtc = DateTime.UtcNow; private DateTime _startedUtc = DateTime.MinValue; private bool _starting; - private bool _simulatorRunning; private bool _lookingAtSceneryFiles; private readonly DispatcherTimer _sceneryFilesTimer = new() { Interval = TimeSpan.FromSeconds(2) }; @@ -116,7 +115,7 @@ public partial class MainWindow : Window /// private async Task LookAtSceneryFilesAsync() { - if (_lookingAtSceneryFiles || _simulatorRunning || !IsVisible || WindowState == WindowState.Minimized) + if (_lookingAtSceneryFiles || AppServices.Current.State.SimulatorRunning || !IsVisible || WindowState == WindowState.Minimized) return; _lookingAtSceneryFiles = true; @@ -690,10 +689,10 @@ public partial class MainWindow : Window return; } - _simulatorRunning = true; + AppServices.Current.State.SimulatorRunning = true; _ = simulator.WaitForExitAsync().ContinueWith(_ => Dispatcher.UIThread.Post(() => { - _simulatorRunning = false; + AppServices.Current.State.SimulatorRunning = false; RestoreFromSimulator(); ReportIfCrashed(simulator, executable, started); }), TaskScheduler.Default);