From 64e43381b96f02721cf7fdb6bf8c0b6d0be2e3c9 Mon Sep 17 00:00:00 2001 From: Martijn Laarman Date: Tue, 16 Jan 2024 09:31:11 +0100 Subject: [PATCH] Return a more usefull LongRunningApplicationSubscription over IDisposable --- src/Proc/Proc.StartLongRunning.cs | 28 +++++++++++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/src/Proc/Proc.StartLongRunning.cs b/src/Proc/Proc.StartLongRunning.cs index 1fd53d6..5d0fab6 100644 --- a/src/Proc/Proc.StartLongRunning.cs +++ b/src/Proc/Proc.StartLongRunning.cs @@ -7,6 +7,28 @@ namespace ProcNet { + public class LongRunningApplicationSubscription : IDisposable + { + internal LongRunningApplicationSubscription(ObservableProcess process, CompositeDisposable subscription) + { + Process = process; + Subscription = subscription; + } + + private IDisposable Subscription { get; } + + public ObservableProcess Process { get; } + + public bool SendControlC(int processId) => Process.SendControlC(processId); + public void SendControlC() => Process.SendControlC(); + + public void Dispose() + { + Subscription?.Dispose(); + Process?.Dispose(); + } + } + public static partial class Proc { @@ -28,7 +50,7 @@ public static partial class Proc /// defaults to which writes standard error messages in red /// /// The exit code and whether the process completed - public static IDisposable StartLongRunning(LongRunningArguments arguments, TimeSpan waitForStartedConfirmation, IConsoleOutWriter consoleOutWriter = null) + public static LongRunningApplicationSubscription StartLongRunning(LongRunningArguments arguments, TimeSpan waitForStartedConfirmation, IConsoleOutWriter consoleOutWriter = null) { var started = false; var confirmWaitHandle = new ManualResetEvent(false); @@ -71,7 +93,7 @@ public static IDisposable StartLongRunning(LongRunningArguments arguments, TimeS else { var completed = confirmWaitHandle.WaitOne(waitForStartedConfirmation); - if (completed) return composite; + if (completed) return new(process, composite); var pwd = arguments.WorkingDirectory; var args = arguments.Args.NaivelyQuoteArguments(); var printBinary = arguments.OnlyPrintBinaryInExceptionMessage @@ -80,7 +102,7 @@ public static IDisposable StartLongRunning(LongRunningArguments arguments, TimeS throw new ProcExecException($"Could not yield started confirmation after {waitForStartedConfirmation} while running {printBinary}"); } - return composite; + return new(process, composite); } } }