From 862525a86fb2ad82c3819ca75c9af6464117dbcb Mon Sep 17 00:00:00 2001 From: JT Date: Thu, 25 May 2023 22:45:48 -0700 Subject: [PATCH] Fix ActiveInstalledPackage --- StabilityMatrix/Helper/ProcessRunner.cs | 1 + StabilityMatrix/PyVenvRunner.cs | 2 +- StabilityMatrix/ViewModels/LaunchViewModel.cs | 34 +++++++++++++------ 3 files changed, 25 insertions(+), 12 deletions(-) diff --git a/StabilityMatrix/Helper/ProcessRunner.cs b/StabilityMatrix/Helper/ProcessRunner.cs index 1c33bd84..9e2eba2c 100644 --- a/StabilityMatrix/Helper/ProcessRunner.cs +++ b/StabilityMatrix/Helper/ProcessRunner.cs @@ -42,6 +42,7 @@ public static class ProcessRunner process.StartInfo.UseShellExecute = false; process.StartInfo.RedirectStandardOutput = true; process.StartInfo.RedirectStandardError = true; + process.StartInfo.RedirectStandardInput = true; process.StartInfo.CreateNoWindow = true; if (environmentVariables != null) diff --git a/StabilityMatrix/PyVenvRunner.cs b/StabilityMatrix/PyVenvRunner.cs index 03844086..c0175d41 100644 --- a/StabilityMatrix/PyVenvRunner.cs +++ b/StabilityMatrix/PyVenvRunner.cs @@ -65,7 +65,7 @@ public class PyVenvRunner: IDisposable } } - public void RunDetached(string arguments, Action outputDataReceived, Action? onExit = null, bool unbuffered = true) + public void RunDetached(string arguments, Action? outputDataReceived, Action? onExit = null, bool unbuffered = true) { if (!Exists()) { diff --git a/StabilityMatrix/ViewModels/LaunchViewModel.cs b/StabilityMatrix/ViewModels/LaunchViewModel.cs index 9f4b8db5..a647f36a 100644 --- a/StabilityMatrix/ViewModels/LaunchViewModel.cs +++ b/StabilityMatrix/ViewModels/LaunchViewModel.cs @@ -22,6 +22,7 @@ public partial class LaunchViewModel : ObservableObject { private readonly ISettingsManager settingsManager; private PyVenvRunner? venvRunner; + private bool clearingPackages = false; [ObservableProperty] private string consoleInput = ""; @@ -44,7 +45,12 @@ public partial class LaunchViewModel : ObservableObject { if (value == selectedPackage) return; selectedPackage = value; - settingsManager.SetActiveInstalledPackage(value); + + if (!clearingPackages) + { + settingsManager.SetActiveInstalledPackage(value); + } + OnPropertyChanged(); } } @@ -56,6 +62,7 @@ public partial class LaunchViewModel : ObservableObject public LaunchViewModel(ISettingsManager settingsManager) { this.settingsManager = settingsManager; + SetProcessRunning(false); } public AsyncRelayCommand LaunchCommand => new(async () => @@ -82,8 +89,8 @@ public partial class LaunchViewModel : ObservableObject { await venvRunner.Setup(); } - - var onConsoleOutput = new Action(s => + + void OnConsoleOutput(string? s) { if (s == null) return; Dispatcher.CurrentDispatcher.Invoke(() => @@ -92,9 +99,9 @@ public partial class LaunchViewModel : ObservableObject ConsoleOutput += s + "\n"; ScrollNeeded?.Invoke(this, EventArgs.Empty); }); - }); - - var onExit = new Action(i => + } + + void OnExit(int i) { Dispatcher.CurrentDispatcher.Invoke(() => { @@ -103,17 +110,16 @@ public partial class LaunchViewModel : ObservableObject ScrollNeeded?.Invoke(this, EventArgs.Empty); SetProcessRunning(false); }); - }); + } var args = "\"" + Path.Combine(packagePath, "launch.py") + "\""; - venvRunner.RunDetached(args, onConsoleOutput, onExit); + venvRunner.RunDetached(args, OnConsoleOutput, OnExit); SetProcessRunning(true); }); public void OnLoaded() { - SetProcessRunning(false); LoadPackages(); if (InstalledPackages.Any() && settingsManager.Settings.ActiveInstalledPackage != null) { @@ -134,9 +140,13 @@ public partial class LaunchViewModel : ObservableObject } [RelayCommand] - private void Stop() + private async Task Stop() { venvRunner?.Dispose(); + if (venvRunner?.Process != null) + { + await venvRunner.Process.WaitForExitAsync(); + } venvRunner = null; SetProcessRunning(false); ConsoleOutput += $"{Environment.NewLine}Stopped process at {DateTimeOffset.Now}{Environment.NewLine}"; @@ -149,13 +159,15 @@ public partial class LaunchViewModel : ObservableObject { return; } - + + clearingPackages = true; InstalledPackages.Clear(); foreach (var package in packages) { InstalledPackages.Add(package); } + clearingPackages = false; } private void SetProcessRunning(bool running)