1 using Microsoft.Extensions.Logging;
4 using System.Threading.Tasks;
12 public int Id {
get; }
15 public Task Startup {
get; }
18 public Task<int> Lifetime {
get; }
31 public Process(
System.Diagnostics.Process handle, Task<int> lifetime, StringBuilder outputStringBuilder, StringBuilder errorStringBuilder, StringBuilder combinedStringBuilder, ILogger<Process> logger,
bool preExisting)
33 this.handle = handle ??
throw new ArgumentNullException(nameof(handle));
34 Lifetime = lifetime ??
throw new ArgumentNullException(nameof(lifetime));
36 this.outputStringBuilder = outputStringBuilder;
37 this.errorStringBuilder = errorStringBuilder;
38 this.combinedStringBuilder = combinedStringBuilder;
40 this.logger = logger ??
throw new ArgumentNullException(nameof(logger));
46 Startup = Task.CompletedTask;
50 Startup = Task.Factory.StartNew(() =>
54 handle.WaitForInputIdle();
56 catch (InvalidOperationException) { }
57 },
default, TaskCreationOptions.LongRunning, TaskScheduler.Current);
59 logger.LogTrace(
"Created process ID: {0}", Id);
63 public void Dispose() => handle.Dispose();
68 if (combinedStringBuilder == null)
69 throw new InvalidOperationException(
"Output/Error reading was not enabled!");
70 return combinedStringBuilder.ToString().TrimStart(Environment.NewLine.ToCharArray());
76 if (errorStringBuilder == null)
77 throw new InvalidOperationException(
"Error reading was not enabled!");
78 return errorStringBuilder.ToString().TrimStart(Environment.NewLine.ToCharArray());
84 if (outputStringBuilder == null)
85 throw new InvalidOperationException(
"Output reading was not enabled!");
86 return errorStringBuilder.ToString().TrimStart(Environment.NewLine.ToCharArray());
96 logger.LogTrace(
"Terminating process...");
102 logger.LogDebug(
"Process termination exception: {0}", e);
110 handle.PriorityClass =
System.Diagnostics.ProcessPriorityClass.AboveNormal;
111 logger.LogTrace(
"Set to above normal priority", handle.Id);
115 logger.LogWarning(
"Unable to raise process priority! Exception: {0}", e);
readonly StringBuilder errorStringBuilder
string GetErrorOutput()
Get the stderr output of the IProcess
Use server authentication
Process(System.Diagnostics.Process handle, Task< int > lifetime, StringBuilder outputStringBuilder, StringBuilder errorStringBuilder, StringBuilder combinedStringBuilder, ILogger< Process > logger, bool preExisting)
readonly StringBuilder combinedStringBuilder
string GetStandardOutput()
Get the stdout output of the IProcess
string GetCombinedOutput()
Get the stderr and stdout output of the IProcess
Abstraction over a System.Diagnostics.Process
void SetHighPriority()
Set's the owned System.Diagnostics.Process.PriorityClass to System.Diagnostics.ProcessPriorityClass.AboveNormal
readonly ILogger< Process > logger
The ILogger for the Process
readonly System.Diagnostics.Process handle
readonly StringBuilder outputStringBuilder
void Terminate()
Terminates the process