Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions docs/core/whats-new/dotnet-10/snippets/csharp/ProcessGroup.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,27 @@ static void Main(string[] args)
using Process process = Process.Start(psi)!;
Thread.Sleep(5_000);

GenerateConsoleCtrlEvent(CTRL_C_EVENT, (uint)process.Id);
GenerateConsoleCtrlEvent(CTRL_BREAK_EVENT, (uint)process.Id);
process.WaitForExit();

Console.WriteLine("Child process terminated gracefully, continue with the parent process logic if needed.");
}
else
{
// If you need to send a CTRL+C, the child process needs to re-enable CTRL+C handling, if you own the code, you can call SetConsoleCtrlHandler(NULL, FALSE).
// CREATE_NEW_PROCESS_GROUP disables CTRL+C handling in the new process group.
// CTRL+BREAK is not affected, so you don't need to re-enable it with SetConsoleCtrlHandler.
// If you use CTRL+C instead, re-enable handling by calling SetConsoleCtrlHandler(NULL, FALSE).
// see https://learn.microsoft.com/windows/win32/api/processthreadsapi/nf-processthreadsapi-createprocessw#remarks
SetConsoleCtrlHandler((IntPtr)null, false);

Console.WriteLine("Greetings from the child process! I need to be gracefully terminated, send me a signal!");

bool stop = false;

var registration = PosixSignalRegistration.Create(PosixSignal.SIGINT, ctx =>
var registration = PosixSignalRegistration.Create(PosixSignal.SIGQUIT, ctx =>
{
stop = true;
ctx.Cancel = true;
Console.WriteLine("Received CTRL+C, stopping...");
Console.WriteLine("Received CTRL+BREAK, stopping...");
});

StreamWriter sw = File.AppendText("log.txt");
Expand Down
Loading