Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Run code on console close?

I am writing a C# app that needs to upload a file when the console is closed (be it via the X button, or the computer is shut down).

How could I do this?

AppDomain.CurrentDomain.ProcessExit += new EventHandler (OnExit);

Only runs when I issue the exit command to the console, not when I hit the red close button.

Please only answer if the solution runs both when the console is closed via the X button, and when the computer is shut down (normally via Windows, I know you can't if the power is pulled xD).

like image 202
rshea0 Avatar asked Mar 27 '12 20:03

rshea0


People also ask

How do you close a console application?

Exit a Console Application With the return Method in C# The return statement ends the execution of a method and returns the control to the calling or the main method. We can use the return statement inside the main() function to end our console application's execution.

Why is the console window closing immediately?

Because it's finished. When console applications have completed executing and return from their main method, the associated console window automatically closes.


2 Answers

You have to invoke the WIN32 API, to do this, just have a look at this post here http://social.msdn.microsoft.com/Forums/en-US/csharpgeneral/thread/707e9ae1-a53f-4918-8ac4-62a1eddb3c4a/

I copied the relevant code for you from there:

class Program

{
    private static bool isclosing = false;

    static void Main(string[] args)
    {
        SetConsoleCtrlHandler(new HandlerRoutine(ConsoleCtrlCheck), true);

        Console.WriteLine("CTRL+C,CTRL+BREAK or suppress the application to exit");

        while (!isclosing) ;
    }

    private static bool ConsoleCtrlCheck(CtrlTypes ctrlType)
    {
        // Put your own handler here

        switch (ctrlType)
        {
            case CtrlTypes.CTRL_CLOSE_EVENT:
                isclosing = true;
                Console.WriteLine("Program being closed!");
                break;
        }

        return true;
    }

    #region unmanaged
    // 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
    }

    #endregion
}

It does exactly what you need.

Greetings,

like image 153
Mario Fraiß Avatar answered Sep 22 '22 14:09

Mario Fraiß


I'll also like to suggest you should save what you want to upload for later so you don't have broken/invalid files uploaded. That way next time the application is started, the upload can be performed. This one at least works for System Logoff/Shutdowns:

SystemEvents.SessionEnding += (SystemEvents_SessionEnding);

private static void SystemEvents_SessionEnding(object sender, SessionEndingEventArgs e)
{
    //code to run on when user is about to logoff or shutdown
}
like image 37
rtuner Avatar answered Sep 22 '22 14:09

rtuner