public sealed class WindowsProcessClient Local process inspection and control. Instances retain no native resources.
Remarks
Snapshot reads are synchronous. Closing, terminating and starting processes are asynchronous: their native calls and waits run off the caller's thread, and cancellation stops waiting without killing, closing or rolling back anything. Process IDs are reused by Windows; act on an ID from a recent snapshot.
Constructors
WindowsProcessClient
public WindowsProcessClient() No documentation.
Methods
CloseOrKillAsync
public Task<ProcessCloseOutcome> CloseOrKillAsync(int processId, TimeSpan timeout, CancellationToken cancellationToken = default) Asks a process to close by posting WM_CLOSE to its visible, unowned top-level windows (the windows a user could close), waits up to timeout for it to exit, and terminates it otherwise.
Exceptions
ArgumentOutOfRangeExceptionprocessIdis negative, ortimeoutis negative or longer than a day.
Remarks
Hidden helper windows and console windows are not asked: closing a console window would signal every process attached to that console. A process without such a window, such as a console program or a service, is terminated at once. Termination uses exit code 1. A process that does not exit within 10 seconds of termination throws TimeoutException. Cancellation stops waiting; it never kills the process.
Enumerate
public ImmutableArray<ProcessSnapshot> Enumerate() Reads a local process snapshot. Failure is never represented as an empty snapshot.
Find
public ProcessSnapshot? Find(int processId) Finds a process in a new snapshot; null means it was absent at observation time.
Exceptions
ArgumentOutOfRangeExceptionprocessIdis negative.
FindByImagePath
public ImmutableArray<ProcessSnapshot> FindByImagePath(string imagePath) Finds the running instances of a program by the full path of its executable, without regard to case.
Exceptions
ArgumentExceptionimagePathis not a full path.
Remarks
Programs that share a file name in different folders are told apart. Processes whose ProcessSnapshot.ImagePath cannot be read are not matched.
GetChildren
public ImmutableArray<ProcessSnapshot> GetChildren(int parentProcessId) Returns immediate children observed in one snapshot.
Exceptions
ArgumentOutOfRangeExceptionparentProcessIdis negative.
StartAndWaitAsync
public Task<int> StartAndWaitAsync(ProcessStartSpecification specification, CancellationToken cancellationToken = default) Starts a program, waits for it to exit and returns its exit code.
Remarks
A program that cannot be started fails with its native error, for example 2 (AdministrationErrorCategory.NotFound) or 1223 when the user declines elevation. Cancellation stops waiting; the program keeps running.
TerminateAsync
public Task<bool> TerminateAsync(int processId, int exitCode = 1, CancellationToken cancellationToken = default) Terminates a process (TerminateProcess, as taskkill /F /PID) and waits for it to exit. Returns false when it was not running.
Exceptions
ArgumentOutOfRangeExceptionprocessIdis negative.
Remarks
A process that does not exit within 10 seconds throws TimeoutException. Once termination has been requested, cancellation no longer applies and the result is returned.