From dfa041dc3303bf69482e4b7b9394fcf1d784a38c Mon Sep 17 00:00:00 2001 From: ChsBuffer <33744752+chsbuffer@users.noreply.github.com> Date: Fri, 12 Nov 2021 19:32:03 +0800 Subject: [PATCH] Revert "Fix Guard close file" --- Netch/Controllers/Guard.cs | 35 +++++++++++++++++------------------ 1 file changed, 17 insertions(+), 18 deletions(-) diff --git a/Netch/Controllers/Guard.cs b/Netch/Controllers/Guard.cs index 09e79796..0f369210 100644 --- a/Netch/Controllers/Guard.cs +++ b/Netch/Controllers/Guard.cs @@ -61,6 +61,13 @@ namespace Netch.Controllers public Process Instance { get; } + ~Guard() + { + _logFileStream?.Dispose(); + _logStreamWriter?.Dispose(); + Instance.Dispose(); + } + protected async Task StartGuardAsync(string argument, ProcessPriorityClass priority = ProcessPriorityClass.Normal) { State = State.Starting; @@ -77,12 +84,8 @@ namespace Netch.Controllers if (RedirectOutput) { - WaitAllReadOutputTaskAsync(new[] - { - ReadOutputAsync(Instance.StandardOutput), - ReadOutputAsync(Instance.StandardError) - }) - .Forget(); + Task.Run(() => ReadOutput(Instance.StandardOutput)).Forget(); + Task.Run(() => ReadOutput(Instance.StandardError)).Forget(); if (!StartedKeywords.Any()) { @@ -112,21 +115,12 @@ namespace Netch.Controllers } } - private async Task WaitAllReadOutputTaskAsync(Task[] tasks) - { - await Task.WhenAll(tasks); - _logStreamWriter?.Close(); - _logFileStream?.Close(); - Instance.Dispose(); - State = State.Stopped; - } - - private async Task ReadOutputAsync(TextReader reader) + private void ReadOutput(TextReader reader) { string? line; - while ((line = await reader.ReadLineAsync()) != null) + while ((line = reader.ReadLine()) != null) { - await _logStreamWriter!.WriteLineAsync(line); + _logStreamWriter!.WriteLine(line); OnReadNewLine(line); if (State == State.Starting) @@ -140,6 +134,8 @@ namespace Netch.Controllers } } } + + State = State.Stopped; } public virtual async Task StopAsync() @@ -149,6 +145,9 @@ namespace Netch.Controllers protected async Task StopGuardAsync() { + _logStreamWriter?.Close(); + _logFileStream?.Close(); + try { if (Instance is { HasExited: false })