[RESOLVED] Ultimate Program Exit Event
Does it exist an even, in a console app, that fires when the following happens:
I exit the program via code
I exit the program by clicking the red X to the top-right
I exit the program by ending the process in Task manager
If so, what is it called?
If not, what is the nearly ultimate exit even?
Re: Ultimate Program Exit Event
Take a look at this blog post.
Re: Ultimate Program Exit Event
Re: Ultimate Program Exit Event
I looked at it but I don't really understand how to use it.
Here's my code:
Code:
#region ConsoleCtrlHandler Declaration
// Declare the SetConsoleCtrlHandler function
// as external and receiving a delegate.
[DllImport("Kernel32")]
public static extern bool SetConsoleCtrlHandler(HandlerRoutine Handler, bool Add);
// A delegate type to be used as the handler routine
// for SetConsoleCtrlHandler.
public delegate bool HandlerRoutine(CtrlTypes CtrlType);
// An enumerated type for the control messages
// sent to the handler routine.
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)
{
// Put your own handler here
return true;
}
#endregion
static void Main(string[] args)
{
SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);
}
If I have understood it right, the "put your own handler here" function should execute when I exit the application, which it doesn't. Am I wrong?
Re: Ultimate Program Exit Event
Check for "Query Unload" event.
And check second argument for "close reason" or something similar
Re: Ultimate Program Exit Event
I dont have visual studio here at the moment but I cant imagine it being any more complex than checking if ctrlType equals CTRL_CLOSE_EVENT in the ConsoleCtrlCheck callback.
Re: Ultimate Program Exit Event
There is one that you can't trap though. You should know about this.
If someone opens Task Manager and goes to the processes tab (not the programs tab) and forcefully terminates the process from there, it is ended without a message to the program's message queue.
That said, it's possible to disable the ability to end via that method. You have to mark the process as critical - a bit of a Google search will bring details about it.
But I wanted you to know about that.
All other program exit methods are trapped through the messaging queue which is what you have set up.
... and yes, the "put your own handler here" part should be executing. How are you sure it's not?
Re: Ultimate Program Exit Event
Yeah assumed there wasn't a event for the Task manager process exit.
I have put a break point on the return true; line, and it doesn't execute.
My code is as above, have I really done everything right?
thx for the tip about protecting the process, that maybe works as a solution too. But would be great if I could learn how this works too, to add some knowledge skillpoints to my brain ;)
Re: Ultimate Program Exit Event
The idea is this:
When your program runs, windows creates a message queue for it. Everything that happens is pumped to that queue. Moving the mouse, clicking on a UI object, or just clicking, pressing keys, the system going to screensaver mode. All sorts of things.
Your program receives those messages and for some, says "yeah whatever". Moving the mouse for instance, in a console app, means nothing to the app (normally). So most console apps just say "yeah whatever".
But some events are significant. Pressing a key for example. Now, the console app has to decide what to do with it. (Or any app - not just console ones.) In this case, it usually becomes a printed character on the console.
Most of these you never worry about as a programmer as one of the advantages of a high level programming language is that they are handled for you and instead, generate events. (Like OnKeyDown) You respond to the events instead.
Program exits are messages too. What you're doing above is saying "Whoa whoa whoa - I know you normally 'do your thing' for these messages, but instead, let me handle them". I'm not sure why you're not getting them, double check the source where you got the code because it appears to be a bit different than handling FORMS based messages.
But in your "whoa let me handle it" code you can do whatever you want - within execution limits. If you take too long to respond, windows will assume you aren't handling the message and since it's an exit request, will show the "this program is not responding" and the user can kill it. This performs the same kill as END PROCESS from the process tab.
END PROCESS does not go through the Windows Messaging Queue. It terminates the process. Cold.
Re: Ultimate Program Exit Event
I solved it, thx for helping me :D
It wasn't anything wrong with the code, the SetCtrlHandler never got executed because another method blocked it.