在分配控制台之前调用 Console.WriteLine

发布于 2024-12-06 04:22:58 字数 417 浏览 0 评论 0原文

我最近在应用程序中遇到了以下问题:尽管已使用 AllocConsole 分配了控制台,但它没有显示任何控制台输出。我很快就发现这是由于在调用 AllocConsole 之前尝试写入控制台(隐藏在代码中)引起的。所以它看起来像这样:

Console.WriteLine("Foo"); // no console allocated yet
AllocConsole();           // console window appears
Console.WriteLine("Bar"); // expecting "Bar" in the console, but the console is blank

所以我的问题是:为什么会发生这种情况?我没有看到任何例外(尽管我认为它们是存在的)。

I've recently encountered the following problem with my application: it didn't show any console output, though the console had been allocated by using AllocConsole. I managed to figure out soon that it was caused by an attempt (hidden deeply in code) to write to the console before the AllocConsole was called. So it looked like this:

Console.WriteLine("Foo"); // no console allocated yet
AllocConsole();           // console window appears
Console.WriteLine("Bar"); // expecting "Bar" in the console, but the console is blank

So my question is: why does this happen? I don't see any exceptions (though I suppose they are there).

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

丢了幸福的猪 2024-12-13 04:22:58

第一次使用 Console.WriteLine 时,Console 类会创建一个 TextWriter 并将其与 Console.Out 关联起来> 财产。它执行此操作的方法是使用 Win32 打开与标准输出文件句柄关联的低级文件句柄。如果标准输出句柄无效,则 Console.Out 将设置为 TextWriter.Null,丢弃所有输出。

Win32 AllocConsole 函数创建并设置标准输出句柄,因此在调用它之后,标准输出句柄要么不同,要么现在有效。无论哪种情况,Console.Out 都已设置为使用标准输出或放弃所有输出。

要在调用 AllocConsole 后强制重新打开 Console.Out,可以使用以下方法:

The first time you use Console.WriteLine, the Console class creates a TextWriter and associates it with the Console.Out property. The way it does this is to use Win32 to open the low-level file handle associated with the standard output file handle. If the standard output handle is invalid, Console.Out is set to TextWriter.Null, which discards all output.

The Win32 AllocConsole function, creates and sets the standard output handle so after calling it the standard output handle is either different or now valid. In either case, Console.Out has already been set either to use the old standard output or to discard all output.

To force a re-open of Console.Out after calling AllocConsole, you can use this method:

尬尬 2024-12-13 04:22:58

可能是因为 Console 类的静态构造函数在您第一次调用 Console.WriteLine 时设置输出流。由于没有连接控制台,因此没有标准输出句柄,因此输出将被路由到位桶。当您稍后调用 AllocConsole 时,Console 类中不会通知控制台现在存在。它没有机会将Console.Out附加到新创建的标准输出句柄。

Probably because the static constructor of the Console class sets up the output stream the first time you call Console.WriteLine. Since there's no console attached, and therefore no standard output handle, output gets routed to the bit bucket. And when you call AllocConsole later, nothing in the Console class is notified that a console now exists. It doesn't have the opportunity to attach Console.Out to the newly created standard output handle.

衣神在巴黎 2024-12-13 04:22:58

一个进程只能与一个控制台关联,因此如果调用进程已有一个控制台AllocConsole 函数将失败>。并且console应用程序已经拥有console。请参阅此处的详细信息

A process can be associated with only one console, so the AllocConsole function fails if the calling process already has a console. And the console application is already has the console. See details in here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文