在分配控制台之前调用 Console.WriteLine
我最近在应用程序中遇到了以下问题:尽管已使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
第一次使用
Console.WriteLine
时,Console
类会创建一个TextWriter
并将其与Console.Out
关联起来> 财产。它执行此操作的方法是使用 Win32 打开与标准输出文件句柄关联的低级文件句柄。如果标准输出句柄无效,则Console.Out
将设置为TextWriter.Null
,丢弃所有输出。Win32
AllocConsole
函数创建并设置标准输出句柄,因此在调用它之后,标准输出句柄要么不同,要么现在有效。无论哪种情况,Console.Out
都已设置为使用旧标准输出或放弃所有输出。要在调用
AllocConsole
后强制重新打开Console.Out
,可以使用以下方法:Console.OpenStandardOutput
The first time you use
Console.WriteLine
, theConsole
class creates aTextWriter
and associates it with theConsole.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 toTextWriter.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 callingAllocConsole
, you can use this method:Console.OpenStandardOutput
可能是因为
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 callConsole.WriteLine
. Since there's no console attached, and therefore no standard output handle, output gets routed to the bit bucket. And when you callAllocConsole
later, nothing in theConsole
class is notified that a console now exists. It doesn't have the opportunity to attachConsole.Out
to the newly created standard output handle.一个进程只能与一个
控制台
关联,因此如果调用进程已有一个控制台
,AllocConsole
函数
将失败>。并且console
应用程序已经拥有console
。请参阅此处的详细信息A process can be associated with only one
console
, so theAllocConsole
function
fails if the calling process already has aconsole
. And theconsole
application is already has theconsole
. See details in here