none
catch console closing from a forms application RRS feed

  • Question

  • I have a forms application where I open a console window.  When I close the console window, it also closes the application.  I would like to catch the console closnig event to prevent the forms application from closing.  I have found how to catch the console closing event (using SetConsoleCtrlHandler(*)), which works fine in a console application, but I haven't been able to get it to work in my forms application.  Does anyone know how to do this if it can be done?  Pleae don't ask why I'm opening a console window from a forms app. Thanks in advance.
    genush
    Friday, January 27, 2012 5:29 PM

Answers

  • Aha! Essently the kernel is hijacking the thread and closing the thread. I dont' think you want that.

    Why not use a Process.Start() or Shell()? Either one will keep your winform open after the console has closed.


    Try to look ahead so you're not forced to look behind.
    • Marked as answer by genush Wednesday, February 1, 2012 4:50 PM
    Friday, January 27, 2012 6:18 PM

All replies

  • Ok I have to ask...what are you opening a console window from a forms app?
    Try to look ahead so you're not forced to look behind.
    Friday, January 27, 2012 5:45 PM
  • In addition to using SetConsoleCtrlHandler (to prevent Ctrl+C), you can also P/Invoke GetConsoleWindow to get the HWND of the console window.  At that point, you can disable the close button on the console entirely, so there is no way to "close" the window.

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, January 27, 2012 5:50 PM
    Moderator
  • I don't want to disable the close button.  I want the user to be able to close the console window so it can be dynamically opened and closed as many times as the user desires.  Again, I'd rather not say why I'm using the console window in this way, but I need to know if it can be closed without automatically closing the main forms app also.
    genush
    Friday, January 27, 2012 5:57 PM
  • I don't want to disable the close button.  I want the user to be able to close the console window so it can be dynamically opened and closed as many times as the user desires.  Again, I'd rather not say why I'm using the console window in this way, but I need to know if it can be closed without automatically closing the main forms app also.
    genush

    You can't change that behavior - the best you could probably do would be to disable the close button, and have your application provide a mechanism to show/hide the console.  You could then call FreeConsole to detach from it as needed.

     

    That being said, if you want to open/close teh console, I'd really, strongly recommend using a Form instead of the console... There are many disadvantages to console windows, and if you're not going to leave the normal behavior, it only makes the disadvantages worse...

     


    Reed Copsey, Jr. - http://reedcopsey.com
    If a post answers your question, please click "Mark As Answer" on that post and "Mark as Helpful".
    Friday, January 27, 2012 6:03 PM
    Moderator
  • Ok,

    All kidding aside, how are you opening the console window?

    Also, in the project itself (somewhere in the properties) you can modify when the applicaiton closes. Off hand, it's something like when main form closes, when any form closes, etc...

    I believe you want "when the main form closes"


    Try to look ahead so you're not forced to look behind.
    Friday, January 27, 2012 6:09 PM
  • I use AllocConsole() from kernel32.dll to open the window.  I have a formclosing event handler for the main form but it is not entered when the application exits by closing the console window.
    genush
    Friday, January 27, 2012 6:14 PM
  • Aha! Essently the kernel is hijacking the thread and closing the thread. I dont' think you want that.

    Why not use a Process.Start() or Shell()? Either one will keep your winform open after the console has closed.


    Try to look ahead so you're not forced to look behind.
    • Marked as answer by genush Wednesday, February 1, 2012 4:50 PM
    Friday, January 27, 2012 6:18 PM