Revert "Fix Guard close file"

This commit is contained in:
ChsBuffer
2021-11-12 19:32:03 +08:00
parent 3f51f7f635
commit dfa041dc33

View File

@@ -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 })