![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#2 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#3 |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5
![]() |
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 |
|
|
|
|
|
#4 |
|
Resident Grouch
![]() ![]() ![]() ![]() ![]() ![]() Join Date: Jun 2005
Posts: 6,453
Rep Power: 10
![]() |
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 |
|
|
|
|
|
#5 |
|
Newbie
Join Date: Oct 2007
Posts: 29
Rep Power: 0
![]() |
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 |
|
|
|
|
|
#6 | |
|
Caffeinated Neural Net
![]() Join Date: Jun 2005
Location: Dry west coast of Canada
Posts: 1,010
Rep Power: 5
![]() |
Re: avoiding application exit caused by console closure
Quote:
__________________
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 |
|
|
|
|
|
|
#7 |
|
Not a user?
Join Date: Sep 2007
Posts: 256
Rep Power: 2
![]() |
Re: avoiding application exit caused by console closure
Maybe you could make the console window an mdi child? No clue, just a thought.
|
|
|
|
|
|
#8 |
|
Newbie
Join Date: Oct 2007
Posts: 29
Rep Power: 0
![]() |
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. |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Manipulating the console using Windows API | rup | C++ | 2 | Nov 17th, 2006 1:04 PM |
| Console Problems | bigguy | Visual Basic .NET | 5 | Sep 24th, 2006 11:34 AM |
| The Black Art of Video Game Console Design | lostcauz | Book Reviews | 0 | Apr 26th, 2006 7:31 PM |
| how to exit an application on error | lucifer | C# | 2 | Nov 29th, 2005 6:27 PM |
| forcing exit in a console program | OpenLoop | C# | 12 | Jun 17th, 2005 12:18 PM |