为什么关闭启动时使用AllocConsole,导致我的整个应用程序退出控制台?我可以改变这种行为? [英] Why does closing a console that was started with AllocConsole cause my whole application to exit? Can I change this behavior?

查看:2345
本文介绍了为什么关闭启动时使用AllocConsole,导致我的整个应用程序退出控制台?我可以改变这种行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想有发生的是,控制台窗口刚刚消失,或者更好的,这是隐藏的,但我想我的应用程序保持运行。那可能吗?我希望能够使用Console.WriteLine和具有控制台成为输出窗口。我希望能够隐藏和显示,而我不希望整个应用程序,只是因为控制台关闭死。

What I want to have happen is that the console window just goes away, or better yet that it is hidden, but I want my application to keep running. Is that possible? I want to be able to use Console.WriteLine and have the console serve as an output window. I want to be able to hide and show it, and I don't want the whole app to die just because the console was closed.

修改

code:

internal class SomeClass {

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

    private static void Main() {
        AllocConsole();
        while(true) continue;
    }
}

编辑2

我试图接受的解决方案在这里[掌握控制台退出C#],每建议在对这一问题的意见。这个例子code被窃听,该dllimport的需要KERNEL32.DLL或KERNEL32,而不是Kernel32中。做出这样的转变后,我得到一个消息,我的处理程序CTRL_CLOSE_EVENT当我点击X控制台窗口。但是,调用FreeConsole和/或返回true并不prevent应用从终止。

I tried the accepted solution here [ Capture console exit C# ], per the suggestion in the comments on this question. The example code is bugged in that the DLLImport needs to be "kernel32.dll" or "kernel32", not "Kernel32". After making that change, I'm getting a message to my handler for CTRL_CLOSE_EVENT when I click the X on the console window. However, calling FreeConsole and/or returning true doesn't prevent the application from terminating.

推荐答案

嗯,是的,这是一个使用Windows控制台子系统的注意事项之一。当用户关闭控制台窗口(不管控制台是如何分配的),所有连接到控制台,该进程被终止的。这种行为使明显的感觉控制台应用程序(即那些专门针对控制台子系统,而不是标准的Windows应用程序),但它可以是像你这样的情况下,一个重大的痛苦。

Ah, yes, this is one of the caveats of using the Windows console subsystem. When the user closes the console window (regardless of how the console was allocated), all of the processes that are attached to the console are terminated. That behavior makes obvious sense for console applications (i.e., those that specifically target the console subsystem, as opposed to standard Windows applications), but it can be a major pain in cases like yours.

这是我知道的唯一的解决方法是使用<一个href="http://msdn.microsoft.com/en-us/library/windows/desktop/ms686016.aspx"><$c$c>SetConsoleCtrlHandler功能,它允许您注册一个处理函数<大骨节病>控制 + <大骨节病> C 和<大骨节病>控制 + <大骨节病>破解信号,以及像关闭控制台窗口用户系统事件,用户注销,或系统关闭。该文件说,如果你只关心无视这些事件,你可以通过第一个参数。例如:

The only workaround that I know of is to use the SetConsoleCtrlHandler function, which allows you to register a handler function for Ctrl+C and Ctrl+Break signals, as well as system events like the user closing the console window, the user logging off, or the system shutting down. The documentation says that if you're only interested in ignoring these events, you can pass null for the first argument. For example:

[DllImport("kernel32")]
static extern bool SetConsoleCtrlHandler(HandlerRoutine HandlerRoutine, bool Add);

delegate bool HandlerRoutine(uint dwControlType);

static void Main()
{
    AllocConsole();
    SetConsoleCtrlHandler(null, true);
    while (true) continue;
}

这完全适用<大骨节病>控制 + <大骨节病> C 和<大骨节病>控制 + <大骨节病>歇信号(这会以其他方式造成的应用程序终止为好),但它不会为一个你问一下,这是 CTRL_CLOSE_EVENT ,由系统生成当用户关闭控制台工作窗口。

That works perfectly for Ctrl+C and Ctrl+Break signals (which would have otherwise caused your application to terminate as well), but it doesn't work for the one you're asking about, which is the CTRL_CLOSE_EVENT, generated by the system when the user closes the console window.

老实说,我不知道该怎么prevent的。即使样品中的人员实际上并没有让你忽略 CTRL_CLOSE_EVENT 。我试图在一个小的测试程序,它的发出哔哔声的当您关闭窗口,然后打印邮件,但这个过程仍然被终止。

Honestly, I don't know how to prevent that. Even the sample in the SDK doesn't actually allow you to ignore the CTRL_CLOSE_EVENT. I tried it in a little test app, and it beeps when you close the window and prints the message, but the process still gets terminated.

或许更令人担忧的是,该文件让我觉得这是不可能的prevent这样的:

Perhaps more worryingly, the documentation makes me think it is not possible to prevent this:

系统生成 CTRL_CLOSE_EVENT CTRL_LOGOFF_EVENT CTRL_SHUTDOWN_EVENT 信号当用户关闭控制台,注销,或关闭系统,使得过程有机会终止之前进行清理。 Console功能,或调用控制台功能,可能没有任何pviously提到的$ P $的三个信号的处理过程中可靠地工作,任何C运行时函数。其原因是,一些内部控制台清除例程或全部可被称为执行处理的信号处理程序之前

The system generates CTRL_CLOSE_EVENT, CTRL_LOGOFF_EVENT, and CTRL_SHUTDOWN_EVENT signals when the user closes the console, logs off, or shuts down the system so that the process has an opportunity to clean up before termination. Console functions, or any C run-time functions that call console functions, may not work reliably during processing of any of the three signals mentioned previously. The reason is that some or all of the internal console cleanup routines may have been called before executing the process signal handler.

这是映入我眼帘的最后一句话。如果控制台子系统启动后本身的清理立即的响应于在尝试关闭窗口的用户时,它可能无法在事后停止它。

It's that last sentence that catches my eye. If the console subsystem starts cleaning up after itself immediately in response to the user attempting to close the window, it may not be possible to halt it after the fact.

(至少现在你明白这个问题。也许别人可以同来解决!)

(At least now you understand the problem. Maybe someone else can come along with a solution!)

这篇关于为什么关闭启动时使用AllocConsole,导致我的整个应用程序退出控制台?我可以改变这种行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆