none
What method gets called when top, right corner close button is clicked of a EXE's console window RRS feed

  • Question

  • Hi folks

    I have an EXE. It opens an Console window, when it runs. The EXE application contains several objects/references to services, which I want to stop gracefully, if the EXE shuts down or closes. For example, if somebody clicks the close button, on the top, RIGHTcorner of the console window (labled as "X"), the EXE stops. What method gets called, when a user clicks on this Close button ? I ask because If I know such method, I can put in the body of such method, code to stop the any services started by the EXE.

    BTW, when my EXE runs the Console Window stays open, becasue i use Console.Readline() as the last code line in the Main method

    Also my EXE code is C Sharp

    Thank you

     


    diana4


    • Edited by Diana4 Sunday, June 19, 2011 3:25 AM
    Friday, June 17, 2011 4:48 AM

Answers

  • Hi Diana

    if i got you right you talk about a console application. If I'm right you might try following:

     

    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          new Program().Run();
        }
    
        public Program()
        {
          Initialize();
        }
    
        ~Program()
        {
          Cleanup();
        }
    
        public void Run()
        {
          // ...
        }
    
        private void Initialize()
        {
          Native.SetConsoleCtrlHandler(new Native.HandlerRoutine(ConsoleEventHandler), true);
          // additional init code
        }
    
        private void Cleanup()
        {
          // Insert your cleanup code
          System.Windows.Forms.MessageBox.Show("Closing");
        }
    
        private bool ConsoleEventHandler(Native.CtrlType ctrlType)
        {
          if (ctrlType == Native.CtrlType.CTRL_CLOSE_EVENT)
          {
            Cleanup();
          }
    
          return true;
        }
      }
    }
    
    And here is the code for 'Native'
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {
      public class Native
      {
        [DllImport("Kernel32")]
        public static extern bool SetConsoleCtrlHandler(HandlerRoutine handler, bool add);
    
        public delegate bool HandlerRoutine(CtrlType ctrlType);
    
        public enum CtrlType
        {
          CTRL_C_EVENT = 0,
          CTRL_BREAK_EVENT,
          CTRL_CLOSE_EVENT,
          CTRL_LOGOFF_EVENT = 5,
          CTRL_SHUTDOWN_EVENT
        }
      }
    }
    

    Oliver

    • Marked as answer by Diana4 Monday, June 20, 2011 1:43 AM
    Friday, June 17, 2011 7:40 AM

All replies

  • Hi Diana

    if i got you right you talk about a console application. If I'm right you might try following:

     

    namespace ConsoleApplication1
    {
      class Program
      {
        static void Main(string[] args)
        {
          new Program().Run();
        }
    
        public Program()
        {
          Initialize();
        }
    
        ~Program()
        {
          Cleanup();
        }
    
        public void Run()
        {
          // ...
        }
    
        private void Initialize()
        {
          Native.SetConsoleCtrlHandler(new Native.HandlerRoutine(ConsoleEventHandler), true);
          // additional init code
        }
    
        private void Cleanup()
        {
          // Insert your cleanup code
          System.Windows.Forms.MessageBox.Show("Closing");
        }
    
        private bool ConsoleEventHandler(Native.CtrlType ctrlType)
        {
          if (ctrlType == Native.CtrlType.CTRL_CLOSE_EVENT)
          {
            Cleanup();
          }
    
          return true;
        }
      }
    }
    
    And here is the code for 'Native'
    using System.Runtime.InteropServices;
    
    namespace ConsoleApplication1
    {
      public class Native
      {
        [DllImport("Kernel32")]
        public static extern bool SetConsoleCtrlHandler(HandlerRoutine handler, bool add);
    
        public delegate bool HandlerRoutine(CtrlType ctrlType);
    
        public enum CtrlType
        {
          CTRL_C_EVENT = 0,
          CTRL_BREAK_EVENT,
          CTRL_CLOSE_EVENT,
          CTRL_LOGOFF_EVENT = 5,
          CTRL_SHUTDOWN_EVENT
        }
      }
    }
    

    Oliver

    • Marked as answer by Diana4 Monday, June 20, 2011 1:43 AM
    Friday, June 17, 2011 7:40 AM
  • Hi Oliver, Any references? Thanks 


     

    diana4


    Sunday, June 19, 2011 6:20 AM
  • Hi Diana,

     

    here's the reference for the 'SetConsoleCtrlHandler'-function
    http://msdn.microsoft.com/en-us/library/ms686016(VS.85).aspx

    ________________________________________

    Oliver

    Monday, June 20, 2011 7:08 AM