Ok so i know that i have to use setconsolehandler() if i want to manage console closing events.
So i have a little of an understanding, but i don't know how to block the CTRL_CLOSE_EVENT, ive tried returning false/true if it catches that event, but no success
Here is what i have so far (thank you Anton Gogolev!)
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
public enum CtrlTypes{
CTRL_C_EVENT = 0,
CTRL_BREAK_EVENT,
CTRL_CLOSE_EVENT,
CTRL_LOGOFF_EVENT = 5,
CTRL_SHUTDOWN_EVENT
}
private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
{
if(ctrlType == CtrlTypes.CTRL_CLOSE_EVENT)
return false;// i have tried true and false and viceversa with the return
// true/false but i cant seem to get it right.
return true;
}
//and then i use this to call it
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
Also is it possible to run a new thread to monitor if the console is closing and block that close if the main thread is in the middle of doing somehting?