From 650f543490a8b26a09e61f97c71ccfe4dbf6a394 Mon Sep 17 00:00:00 2001 From: Difegue Date: Thu, 16 May 2019 00:03:31 +0200 Subject: [PATCH] Kill linux processes properly + more fixes --- Karen/App.config | 3 ++ Karen/App.xaml.cs | 13 +++++-- Karen/Interop/WslDistro.cs | 27 +++++++++---- Karen/MainWindow.xaml | 14 +------ Karen/MainWindow.xaml.cs | 1 + Karen/Properties/AssemblyInfo.cs | 2 +- Karen/Properties/Resources.Designer.cs | 53 +++++++++++++------------- Karen/Properties/Resources.resx | 16 +++++--- Karen/Properties/Settings.Designer.cs | 12 ++++++ Karen/Properties/Settings.settings | 3 ++ 10 files changed, 88 insertions(+), 56 deletions(-) diff --git a/Karen/App.config b/Karen/App.config index 2f4c985..017fd36 100644 --- a/Karen/App.config +++ b/Karen/App.config @@ -22,6 +22,9 @@ 3000 + + True + \ No newline at end of file diff --git a/Karen/App.xaml.cs b/Karen/App.xaml.cs index 8c8dfaf..2a3edd5 100644 --- a/Karen/App.xaml.cs +++ b/Karen/App.xaml.cs @@ -10,7 +10,12 @@ namespace Karen public partial class App : Application { private TaskbarIcon notifyIcon; - public WslDistro Distro { get; private set; } + public WslDistro Distro { get; set; } + + public void ToastNotification(string text) + { + notifyIcon.ShowBalloonTip("LANraragi", text, notifyIcon.Icon); + } protected override void OnStartup(StartupEventArgs e) { @@ -22,11 +27,13 @@ namespace Karen Distro = new WslDistro(); //check if server starts with app - if (Karen.Properties.Settings.Default.StartServerAutomatically) + if (Karen.Properties.Settings.Default.StartServerAutomatically && Distro.Status == AppStatus.Stopped) { - notifyIcon.ShowBalloonTip("LANraragi", "LANraragi is starting automagically...", notifyIcon.Icon); + ToastNotification("LANraragi is starting automagically..."); Distro.StartApp(); } + else + ToastNotification("The Launcher is now running! Please click the icon in your Taskbar."); } protected override void OnExit(ExitEventArgs e) diff --git a/Karen/Interop/WslDistro.cs b/Karen/Interop/WslDistro.cs index 57ee635..b082021 100644 --- a/Karen/Interop/WslDistro.cs +++ b/Karen/Interop/WslDistro.cs @@ -19,7 +19,6 @@ namespace Karen.Interop /// public class WslDistro { - const string DISTRO_NAME = "lanraragi"; public AppStatus Status { get; private set; } public String Version { get; private set; } @@ -57,7 +56,7 @@ namespace Karen.Interop while (!proc.StandardOutput.EndOfStream) { string line = proc.StandardOutput.ReadLine(); - if (line.Replace("\0","").Contains(DISTRO_NAME)) + if (line.Replace("\0","").Contains(Properties.Resources.DISTRO_NAME)) { return true; } @@ -82,7 +81,7 @@ namespace Karen.Interop StartInfo = new ProcessStartInfo { FileName = "wsl.exe", - Arguments = "-d "+DISTRO_NAME+" --exec " + oneLiner, + Arguments = "-d "+ Properties.Resources.DISTRO_NAME + " --exec " + oneLiner, UseShellExecute = false, RedirectStandardOutput = true, CreateNoWindow = true, @@ -148,7 +147,7 @@ namespace Karen.Interop // Start process in WSL and hook up handles // This will direct WSL output to the new console window, or to Visual Studio if running with the debugger attached. // See https://stackoverflow.com/questions/15604014/no-console-output-when-using-allocconsole-and-target-architecture-x86 - WslApi.WslLaunch(DISTRO_NAME, command, false, stdIn,stdOut,stdError, out _lrrHandle); + WslApi.WslLaunch(Properties.Resources.DISTRO_NAME, command, false, stdIn,stdOut,stdError, out _lrrHandle); // Get Process ID of the returned procHandle int lrrId = GetProcessId(_lrrHandle); @@ -162,15 +161,27 @@ namespace Karen.Interop } public bool? StopApp() - { - // Ensure child unix processes are killed as well - + { // Kill WSL Process if (_lrrProc != null && !_lrrProc.HasExited) + { _lrrProc.Kill(); - // No need to remove the attached console here in case the app is restarted later down + // Ensure child unix processes are killed as well by killing the distro. This is only possible on 1809 and up. + new Process + { + StartInfo = new ProcessStartInfo + { + FileName = "wslconfig.exe", + Arguments = "/terminate " + Properties.Resources.DISTRO_NAME, + UseShellExecute = false, + RedirectStandardOutput = true, + CreateNoWindow = true, + } + }.Start(); + } + // No need to remove the attached console here in case the app is restarted later down Status = AppStatus.Stopped; return _lrrProc?.HasExited; } diff --git a/Karen/MainWindow.xaml b/Karen/MainWindow.xaml index e53b532..dea1ee2 100644 --- a/Karen/MainWindow.xaml +++ b/Karen/MainWindow.xaml @@ -4,23 +4,11 @@ xmlns:emoji="clr-namespace:Emoji.Wpf;assembly=Emoji.Wpf" x:Class="Karen.MainWindow" xmlns:p="clr-namespace:Karen.Properties" - Title="LANraragi Windows Settings" Height="650" Width="360" + Title="LANraragi Windows Settings" Height="600" Width="360" Closing="OnClosing"> - - - You can use those buttons to install a new LANraragi package on your machine, or uninstall the current one. - - -