2using System.Diagnostics;
4using System.Threading.Tasks;
6using Microsoft.Extensions.Logging;
7using Microsoft.Win32.SafeHandles;
17 public int Id {
get; }
38 readonly global::System.Diagnostics.Process
handle;
68 global::System.Diagnostics.Process
handle,
75 this.handle =
handle ??
throw new ArgumentNullException(nameof(
handle));
87 this.logger =
logger ??
throw new ArgumentNullException(nameof(
logger));
97 Startup = Task.Factory.StartNew(
102 handle.WaitForInputIdle();
106 logger.LogTrace(ex,
"WaitForInputIdle() failed, this is normal.");
109 CancellationToken.None,
111 TaskScheduler.Current);
113 logger.LogTrace(
"Created process ID: {pid}",
Id);
119 logger.LogTrace(
"Disposing PID {pid}...",
Id);
133 throw new InvalidOperationException(
"Output/Error stream reading was not enabled!");
142 logger.LogTrace(
"PID {pid} already exited",
Id);
148 logger.LogTrace(
"Terminating PID {pid}...",
Id);
150 if (!
handle.WaitForExit(5000))
151 logger.LogWarning(
"WaitForExit() on PID {pid} timed out!",
Id);
155 logger.LogDebug(e,
"PID {pid} termination exception!",
Id);
162 var targetPriority = higher ? ProcessPriorityClass.AboveNormal : ProcessPriorityClass.BelowNormal;
165 handle.PriorityClass = targetPriority;
166 logger.LogTrace(
"Set PID {pid} to {targetPriority} priority",
Id, targetPriority);
170 logger.LogWarning(ex,
"Unable to set priority for PID {id} to {targetPriority}!",
Id, targetPriority);
180 logger.LogTrace(
"Suspended PID {pid}",
Id);
184 logger.LogError(e,
"Failed to suspend PID {pid}!",
Id);
195 logger.LogTrace(
"Resumed PID {pid}",
Id);
199 logger.LogError(e,
"Failed to resume PID {pid}!",
Id);
208 logger.LogTrace(
"PID {pid} Username: {username}",
Id, result);
213 public Task
CreateDump(
string outputFile, CancellationToken cancellationToken)
215 ArgumentNullException.ThrowIfNull(outputFile);
217 logger.LogTrace(
"Dumping PID {pid} to {dumpFilePath}...",
Id, outputFile);
228 var exitCode = await lifetimeTask;
229 logger.LogTrace(
"PID {pid} exited with code {exitCode}",
Id, exitCode);
IIOManager that resolves paths to Environment.CurrentDirectory.
const TaskCreationOptions BlockingTaskCreationOptions
The TaskCreationOptions used to spawn Tasks for potentially long running, blocking operations.
async Task< int > WrapLifetimeTask(Task< int > lifetimeTask)
Attaches a log message to the process' exit event.
async ValueTask DisposeAsync()
readonly global::System.Diagnostics.Process handle
The global::System.Diagnostics.Process object.
readonly Task< string > readTask
The Task<TResult> resulting in the process' standard output/error text.
void AdjustPriority(bool higher)
Set's the owned global::System.Diagnostics.Process.PriorityClass to a non-normal value.
Process(IProcessFeatures processFeatures, global::System.Diagnostics.Process handle, CancellationTokenSource readerCts, Task< int > lifetime, Task< string > readTask, ILogger< Process > logger, bool preExisting)
Initializes a new instance of the Process class.
Task< string > GetCombinedOutput(CancellationToken cancellationToken)
Get the stderr and stdout output of the IProcess. A Task<TResult> resulting in the stderr and stdout ...
void Terminate()
Asycnhronously terminates the process. To ensure the IProcess has ended, use the IProcessBase....
void Suspend()
Suspends the process.
async Task< string > GetExecutingUsername(CancellationToken cancellationToken)
Get the name of the account executing the IProcess. A Task<TResult> resulting in the name of the acco...
readonly ILogger< Process > logger
The ILogger for the Process.
Task< int > Lifetime
The Task<TResult> resulting in the exit code of the process.
readonly SafeProcessHandle safeHandle
The global::System.Diagnostics.Process.SafeHandle.
readonly IProcessFeatures processFeatures
The IProcessFeatures for the Process.
Task Startup
The Task representing the time until the IProcess becomes "idle".
void Resume()
Resumes the process.
Task CreateDump(string outputFile, CancellationToken cancellationToken)
Create a dump file of the process. A Task representing the running operation.
readonly CancellationTokenSource readerCts
The CancellationTokenSource used to shutdown the readTask.
Abstraction for suspending and resuming processes.
void SuspendProcess(global::System.Diagnostics.Process process)
Suspend a given process .
Task CreateDump(global::System.Diagnostics.Process process, string outputFile, CancellationToken cancellationToken)
Create a dump file for a given process .
void ResumeProcess(global::System.Diagnostics.Process process)
Resume a given suspended Process.
Task< string > GetExecutingUsername(global::System.Diagnostics.Process process, CancellationToken cancellationToken)
Get the name of the user executing a given process .
Abstraction over a global::System.Diagnostics.Process.