Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Nov 14th, 2007, 6:48 PM   #1
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,200
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Nov 14th, 2007, 11:13 PM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 14th, 2007, 11:56 PM   #3
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,200
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Nov 15th, 2007, 12:40 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 6,453
Rep Power: 10 DaWei is on a distinguished road
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.
__________________
Abstraction doesn't make it impossible to write bad code; it makes it possible to write superior code.
Contributor's Corner: Grumpy on C++ Exceptions DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 15th, 2007, 8:25 AM   #5
Alias
Programmer
 
Join Date: Oct 2007
Posts: 32
Rep Power: 0 Alias is on a distinguished road
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
Alias is offline   Reply With Quote
Old Nov 15th, 2007, 4:16 PM   #6
lectricpharaoh
SEXY SHOELESS GOD OF WAR!
 
lectricpharaoh's Avatar
 
Join Date: Jun 2005
Location: Wet west coast of Canada
Posts: 1,200
Rep Power: 5 lectricpharaoh will become famous soon enough
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.
__________________
And once again, Probability proves itself willing to sneak into a back alley and service Drama as would a copper-piece harlot.
- Vaarsuvius, Order of the Stick
lectricpharaoh is online now   Reply With Quote
Old Nov 15th, 2007, 6:25 PM   #7
Jabo
Not a user?
 
Join Date: Sep 2007
Posts: 308
Rep Power: 2 Jabo is on a distinguished road
Re: avoiding application exit caused by console closure

Maybe you could make the console window an mdi child? No clue, just a thought.
Jabo is offline   Reply With Quote
Old Nov 15th, 2007, 7:31 PM   #8
Alias
Programmer
 
Join Date: Oct 2007
Posts: 32
Rep Power: 0 Alias is on a distinguished road
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.
Alias is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Manipulating the console using Windows API rup C++ 2 Nov 17th, 2006 2:04 PM
Console Problems bigguy Visual Basic .NET 5 Sep 24th, 2006 12:34 PM
The Black Art of Video Game Console Design lostcauz Book Reviews 0 Apr 26th, 2006 8:31 PM
how to exit an application on error lucifer C# 2 Nov 29th, 2005 7:27 PM
forcing exit in a console program OpenLoop C# 12 Jun 17th, 2005 1:18 PM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 12:03 PM.

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