/// <summary> /// Construct a <see cref="Process"/> /// </summary> /// <param name="handle">The value of <see cref="handle"/></param> /// <param name="lifetime">The value of <see cref="Lifetime"/></param> /// <param name="outputStringBuilder">The value of <see cref="outputStringBuilder"/></param> /// <param name="errorStringBuilder">The value of <see cref="errorStringBuilder"/></param> /// <param name="combinedStringBuilder">The value of <see cref="combinedStringBuilder"/></param> /// <param name="logger">The value of <see cref="logger"/></param> /// <param name="preExisting">If <paramref name="handle"/> was NOT just created</param> public Process( global::System.Diagnostics.Process handle, Task <int> lifetime, StringBuilder outputStringBuilder, StringBuilder errorStringBuilder, StringBuilder combinedStringBuilder, ILogger <Process> logger, bool preExisting) { this.handle = handle ?? throw new ArgumentNullException(nameof(handle)); this.outputStringBuilder = outputStringBuilder; this.errorStringBuilder = errorStringBuilder; this.combinedStringBuilder = combinedStringBuilder; this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); Lifetime = WrapLifetimeTask(lifetime ?? throw new ArgumentNullException(nameof(lifetime))); Id = handle.Id; if (preExisting) { Startup = Task.CompletedTask; return; } Startup = Task.Factory.StartNew(() => { try { handle.WaitForInputIdle(); } catch (InvalidOperationException) { } }, default, TaskCreationOptions.LongRunning, TaskScheduler.Current);
/// <summary> /// Construct a <see cref="Process"/> /// </summary> /// <param name="processFeatures">The value of <see cref="processFeatures"/></param> /// <param name="handle">The value of <see cref="handle"/></param> /// <param name="lifetime">The value of <see cref="Lifetime"/></param> /// <param name="standardOutputTask">The value of <see cref="standardOutputTask"/></param> /// <param name="standardErrorTask">The value of <see cref="standardErrorTask"/></param> /// <param name="combinedStringBuilder">The value of <see cref="combinedStringBuilder"/></param> /// <param name="logger">The value of <see cref="logger"/></param> /// <param name="preExisting">If <paramref name="handle"/> was NOT just created</param> public Process( IProcessFeatures processFeatures, global::System.Diagnostics.Process handle, Task <int> lifetime, Task <string> standardOutputTask, Task <string> standardErrorTask, StringBuilder combinedStringBuilder, ILogger <Process> logger, bool preExisting) { this.handle = handle ?? throw new ArgumentNullException(nameof(handle)); // Do this fast because the runtime will bitch if we try to access it after it ends Id = handle.Id; // https://stackoverflow.com/a/47656845 safeHandle = handle.SafeHandle; this.processFeatures = processFeatures ?? throw new ArgumentNullException(nameof(processFeatures)); this.standardOutputTask = standardOutputTask; this.standardErrorTask = standardErrorTask; this.combinedStringBuilder = combinedStringBuilder; this.logger = logger ?? throw new ArgumentNullException(nameof(logger)); Lifetime = WrapLifetimeTask(lifetime ?? throw new ArgumentNullException(nameof(lifetime))); if (preExisting) { Startup = Task.CompletedTask; return; } Startup = Task.Factory.StartNew( () => { try { handle.WaitForInputIdle(); } catch (Exception ex) { logger.LogDebug(ex, "Error on WaitForInputIdle()!"); } },