Programming Forums

Programming Forums (http://www.programmingforums.org/forumindex.php)
-   C# (http://www.programmingforums.org/forum16.html)
-   -   avoiding application exit caused by console closure (http://www.programmingforums.org/showthread.php?t=14451)

lectricpharaoh Nov 14th, 2007 6:48 PM

avoiding application exit caused by console closure
 
I'm working on a Windows GUI app, but I also want to use a console in it. This is easy enough using p/invoke to call AllocConsole(), but the problem is that when the console window is closed, it closes my entire application. Handling the FormClosing event from my form doesn't do any good, as it only works if I close the form via its UI, or by calling the Close() method.

Is there a way to detect when the console is closed, and handle this condition appropriately, rather than exiting the entire application? I've looked into the Console class, but the only event it raises is CancelKeyPress to handle ctrl-C conditions.

DaWei Nov 14th, 2007 11:13 PM

Re: avoiding application exit caused by console closure
 
What's closing the console window that you can't control? I can't speak for what C# gives you access to, but the Windows API will notify you of many extraneous events such as clicking the big X, logoff, ctrl-break, and many others.

Some of these you can trap and stymie, some you can't. Which is as it should be.

lectricpharaoh Nov 14th, 2007 11:56 PM

Re: avoiding application exit caused by console closure
 
The app isn't a console-based one. It's a Windows forms app that allocates a console to display some output, but once the console window opens up, there are two windows. I'd like the app to exit when the user clicks the close button of the GUI, but when the close button of the console window is clicked, I'd like a mechanism for detecting this, and simply closing the console window, rather than both.

The problem, however, is that clicking the console's close button doesn't raise any events, so there's nothing to intercept.

Basically, what I want are two mechanisms to close the console window. The first is through code, by calling FreeConsole(). The second, more intuitive mechanism, is for the user to close the console by clicking the close button.

DaWei Nov 15th, 2007 12:40 AM

Re: avoiding application exit caused by console closure
 
As I said, I don't have any idea how C# incorporates this stuff, but the underlying API sends an event when the big X is clicked.

Alias Nov 15th, 2007 8:25 AM

Re: avoiding application exit caused by console closure
 
Maybe the SetConsoleCtrlHandler function can help you in this instance. With this you can attach an event handler defined in your application.

MSDN Info

lectricpharaoh Nov 15th, 2007 4:16 PM

Re: avoiding application exit caused by console closure
 
Quote:

Originally Posted by Alias
Maybe the SetConsoleCtrlHandler function can help you in this instance. With this you can attach an event handler defined in your application.

That might work, except I don't know how to set up a managed method to be called from unmanaged code. Time to play around, and see if it works. Thanks for the tip.

Jabo Nov 15th, 2007 6:25 PM

Re: avoiding application exit caused by console closure
 
Maybe you could make the console window an mdi child? No clue, just a thought.

Alias Nov 15th, 2007 7:31 PM

Re: avoiding application exit caused by console closure
 
Hmm, I am a little puzzled on this too so I took a look, this is what I have but it still fails with the effect you are trying to correct:

EDIT: I tell a lie in the codes notes, I tested again after posting and I did not get the null reference exception, I presume it must of been something else in my code before alterations.

:

 
public partial class Test : Form
    {
        public Test()
        {
            InitializeComponent();
            AllocConsole();
            //with the following line noted-in
            //we generate a null reference exception
            //I think because we still have the initial
            //problem, resources are freed
            //SetConsoleCtrlHandler(new ConsoleEventHandler(OnConsoleEvent), true);
        }

        [DllImport("kernel32.dll")]
        private static extern bool AllocConsole();

        [DllImport("kernel32.dll")]
        private static extern bool SetConsoleCtrlHandler(ConsoleEventHandler e, bool add);       

        public delegate void ConsoleEventHandler(ConsoleEvent e);
        public enum ConsoleEvent
        {
            CTRL_C = 0,
            CTRL_BREAK = 1,
            CTRL_CLOSE = 2,
            CTRL_LOGOFF = 5,
            CTRL_SHUTDOWN = 6
        }     

        private static void OnConsoleEvent(ConsoleEvent e)
        {
            MessageBox.Show(Enum.GetName(typeof(ConsoleEvent), e));
        }
    }


Cannot hang around tonight, but I hope this helps you progress a little.


All times are GMT -5. The time now is 3:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC