Last active
August 19, 2021 07:20
-
-
Save rasmuseeg/ea2760826378f78c0471caef8968174b to your computer and use it in GitHub Desktop.
The following example allows .NET 4.7.2 console applications to listen for SIGINT, SIGTERM inside windows containers.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# This causes Windows to wait the specified number of seconds before sending CTRL_SHUTDOWN_EVENT to the process. | |
# #RUN reg add hklm\system\currentcontrolset\services\cexecsvc /v ProcessShutdownTimeoutSeconds /t REG_DWORD /d 7200 | |
# This causes Windows to wait the specified number of milliseconds before killing a process after issuing a CTRL_SHUTDOWN_EVENT - essentially the rough equivalent of docker stop -t. | |
RUN reg add hklm\system\currentcontrolset\control /v WaitToKillServiceTimeout /t REG_SZ /d 7200000 /f |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Runtime.InteropServices; | |
using System.Threading; | |
namespace delayed_shutdown | |
{ | |
class Program | |
{ | |
public enum CtrlTypes | |
{ | |
CTRL_C_EVENT = 0, | |
CTRL_BREAK_EVENT, | |
CTRL_CLOSE_EVENT, | |
CTRL_LOGOFF_EVENT = 5, | |
CTRL_SHUTDOWON_EVENT | |
} | |
[DllImport("Kernel32")] | |
public static extern bool SetConsoleCtrlHandler(HandlerRoutine handler, bool Add); | |
public delegate bool HandlerRoutine(CtrlTypes CtrlType); | |
public static volatile HandlerRoutine handlerRoutine = new HandlerRoutine(ConsoleCtrlCheck), true) | |
public static volatile ManualResetEvent exitEvent = new ManualResetEvent(false); | |
public static bool ConsoleCtrlCheck(CtrlTypes ctrlType) | |
{ | |
switch (ctrlType) | |
{ | |
case CtrlTypes.CTRL_C_EVENT: | |
Console.WriteLine("CTRL_C received"); | |
exitEvent.Set(); | |
return true; | |
case CtrlTypes.CTRL_CLOSE_EVENT: | |
Console.WriteLine("CTRL_CLOSE received"); | |
exitEvent.Set(); | |
return true; | |
case CtrlTypes.CTRL_BREAK_EVENT: | |
Console.WriteLine("CTRL_BREAK received"); | |
exitEvent.Set(); | |
return true; | |
case CtrlTypes.CTRL_LOGOFF_EVENT: | |
Console.WriteLine("CTRL_LOGOFF received"); | |
exitEvent.Set(); | |
return true; | |
case CtrlTypes.CTRL_SHUTDOWON_EVENT: | |
Console.WriteLine("CTRL_SHUTDOWN received"); | |
exitEvent.Set(); | |
return true; | |
default: | |
return false; | |
} | |
} | |
static int Main(string[] args) | |
{ | |
if (!SetConsoleCtrlHandler(handlerRoutine)) | |
{ | |
Console.WriteLine("Error setting up control handler... :("); | |
return -1; | |
} | |
Console.WriteLine("Waiting for control event..."); | |
exitEvent.WaitOne(); | |
var i = 60; | |
Console.WriteLine($"Exiting in {i} seconds..."); | |
while (i > 0) | |
{ | |
Console.WriteLine($"{i}"); | |
Thread.Sleep(TimeSpan.FromSeconds(1)); | |
i--; | |
} | |
Console.WriteLine("Goodbye"); | |
return 0; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment