Background and motivation
Process.Dispose only releases associated resources (handles) without terminating the child process. This frequently leads to leaked child processes when developers use the natural using pattern:
using Process process = Process.Start("long-running-tool")!;
// ... use the process ...
// process.Dispose() is called here, but the child process keeps running
Today, to ensure a child process is killed on dispose, users must write manual try/finally blocks:
using Process process = Process.Start("long-running-tool")!;
try
{
// ... use the process ...
}
catch
{
process.Kill(entireProcessTree: true);
throw;
}
A ProcessStartInfo.DisposeBehavior property would allow the using pattern to work as users intuitively expect and eliminate an entire class of leaked-process bugs.
API Proposal
namespace System.Diagnostics
{
public enum ProcessDisposeBehavior
{
None = 0,
KillProcess = 1,
KillProcessTree = 2
}
public partial class ProcessStartInfo
{
public ProcessDisposeBehavior DisposeBehavior { get; set; }
}
}
API Usage
Basic usage — kill a long-running process tree when done:
using Process process = Process.Start(new ProcessStartInfo("dev-server")
{
DisposeBehavior = ProcessDisposeBehavior.KillProcessTree
})!;
// ... interact with the dev server ...
// process.Dispose() is called here, which kills the process tree first
Background and motivation
Process.Disposeonly releases associated resources (handles) without terminating the child process. This frequently leads to leaked child processes when developers use the naturalusingpattern:Today, to ensure a child process is killed on dispose, users must write manual try/finally blocks:
A
ProcessStartInfo.DisposeBehaviorproperty would allow theusingpattern to work as users intuitively expect and eliminate an entire class of leaked-process bugs.API Proposal
API Usage
Basic usage — kill a long-running process tree when done: