Class AsyncProcessRunner
Runs a process asynchronously in the background. Supports monitoring state while running, startup/run timeouts, and process control.
Inherited Members
Namespace: Benday.Common
Assembly: Benday.Common.dll
Syntax
public class AsyncProcessRunner : IAsyncProcessRunner, IDisposable
Constructors
| Edit this page View SourceAsyncProcessRunner(ProcessStartInfo)
Creates a new instance of AsyncProcessRunner.
Declaration
public AsyncProcessRunner(ProcessStartInfo startInfo)
Parameters
| Type | Name | Description |
|---|---|---|
| ProcessStartInfo | startInfo | The ProcessStartInfo to configure the process. |
Properties
| Edit this page View SourceErrorText
Get the current error text. Thread-safe, can be called while process is running.
Declaration
public string ErrorText { get; }
Property Value
| Type | Description |
|---|---|
| string |
ExitCode
The exit code from the process. Returns -1 if not yet completed.
Declaration
public int ExitCode { get; }
Property Value
| Type | Description |
|---|---|
| int |
HasCompleted
Indicates if the process has completed.
Declaration
public bool HasCompleted { get; }
Property Value
| Type | Description |
|---|---|
| bool |
HasStarted
Indicates if the process has been started.
Declaration
public bool HasStarted { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsError
Indicates if the process completed with an error.
Declaration
public bool IsError { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsRunning
Indicates if the process is currently running.
Declaration
public bool IsRunning { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsSuccess
Indicates if the process completed successfully.
Declaration
public bool IsSuccess { get; }
Property Value
| Type | Description |
|---|---|
| bool |
IsTimeout
Indicates if the process timed out.
Declaration
public bool IsTimeout { get; }
Property Value
| Type | Description |
|---|---|
| bool |
OutputText
Get the current output text. Thread-safe, can be called while process is running.
Declaration
public string OutputText { get; }
Property Value
| Type | Description |
|---|---|
| string |
ProcessId
The process ID. Returns null if process has not started.
Declaration
public int? ProcessId { get; }
Property Value
| Type | Description |
|---|---|
| int? |
RunTimeout
The timeout in milliseconds for the overall process run duration. Default is 0 (no timeout - runs indefinitely).
Declaration
public int RunTimeout { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
StartInfo
The ProcessStartInfo object used to configure the process.
Declaration
public ProcessStartInfo StartInfo { get; }
Property Value
| Type | Description |
|---|---|
| ProcessStartInfo |
StartupTimeout
The timeout in milliseconds for the process startup. Default is 5000ms (5 seconds).
Declaration
public int StartupTimeout { get; set; }
Property Value
| Type | Description |
|---|---|
| int |
UnderlyingProcess
Direct access to the underlying Process instance.
Declaration
public Process? UnderlyingProcess { get; }
Property Value
| Type | Description |
|---|---|
| Process |
Methods
| Edit this page View SourceDispose()
Disposes of the AsyncProcessRunner and kills the process if still running.
Declaration
public void Dispose()
Dispose(bool)
Disposes of managed and unmanaged resources.
Declaration
protected virtual void Dispose(bool disposing)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | disposing | True if called from Dispose(), false if from finalizer. |
Kill(bool)
Kill/stop the running process.
Declaration
public void Kill(bool entireProcessTree = false)
Parameters
| Type | Name | Description |
|---|---|---|
| bool | entireProcessTree | If true, kills the entire process tree. |
StartAsync(CancellationToken)
Start the process in the background (non-blocking).
Declaration
public Task StartAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken | Optional cancellation token. |
Returns
| Type | Description |
|---|---|
| Task | Task that completes when the process has started. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown if StartAsync() is called more than once. |
| TimeoutException | Thrown if the process does not start within StartupTimeout. |
WaitForExitAsync(CancellationToken)
Wait for the process to complete.
Declaration
public Task WaitForExitAsync(CancellationToken cancellationToken = default)
Parameters
| Type | Name | Description |
|---|---|---|
| CancellationToken | cancellationToken | Optional cancellation token. |
Returns
| Type | Description |
|---|---|
| Task | Task that completes when the process has finished. |