I have created a console application which has a method that stores kinect stream to hard disk. I want to capture console exit event in order to close the kinect stream since I have issues when the console exit from close event. I want to find a way to detect the exit event of the console. I came across solutions like the following one. I add the following code to my app:
[DllImport("Kernel32")]
private static extern bool SetConsoleCtrlHandler(EventHandler handler, bool add);
private delegate bool EventHandler(CtrlType sig);
static EventHandler _handler;
enum CtrlType
{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT = 1,
CTRL_CLOSE_EVENT = 2,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT = 6
}
private static bool Handler(CtrlType sig)
{
switch (sig)
{
case CtrlType.CTRL_C_EVENT:
case CtrlType.CTRL_LOGOFF_EVENT:
case CtrlType.CTRL_SHUTDOWN_EVENT:
case CtrlType.CTRL_CLOSE_EVENT:
default:
return false;
}
}
And in the main function:
Program obj = new Program(dirPath + args[3] + "_" + args[4]);
_handler += new EventHandler(Handler);
SetConsoleCtrlHandler(_handler, true);
Console.ReadLine();
I am wandering firstly if I can import handler event to a boolean variable and secondly how can I give it as an input to program constructor in order to close my stream.
EDIT: My program constructor:
public Program(string filePath)
{
Thread thread = new Thread(() => writeRGSStream(file));
thread.Start();
writeSkelFiles(file);
}
I want to give the flag input inside the writeRGSStream method. Can I call
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
inside that method in order to get the flag value?