Skip to content

[API Proposal]: Add API to gracefully terminate process #109432

Open
@tmat

Description

@tmat

Background and motivation

dotnet-watch needs to be able to terminate process gracefully, i.e. give it a chance to clean up resources.

On Windows this API would call GenerateConsoleCtrlEvent, on Unix it would send SIGTERM signal.
Currently this requires reimplementing both process creation and termination using P/Invokes.

API Proposal

Multiple options. One option is to add parameter to Process.Kill that indicates whether we want forced kill or "soft" termination (Ctrl+C/SIGTERM).

Process.Kill(bool entireProcessTree, bool forced = true);

ProcessStartInfo might need a bool property, say EnableConsoleControlEvents that allows Kill to send Ctrl+C.

API Usage

var process = new Process
{
    StartInfo =
    {
        EnableConsoleControlEvents = true
        // file name, arguments, output redirection, etc.
    }
};

process.Start();

process.Kill(entireProcessTree: false, forced: false);

process.WaitForExit();

Alternative Designs

Related: #71515

Risks

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    api-suggestionEarly API idea and discussion, it is NOT ready for implementationarea-System.Diagnostics.Processhelp wanted[up-for-grabs] Good issue for external contributors

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions