![]() |
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. |
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. |
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. |
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.
|
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 |
Re: avoiding application exit caused by console closure
Quote:
|
Re: avoiding application exit caused by console closure
Maybe you could make the console window an mdi child? No clue, just a thought.
|
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. :
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