附加到 VS 中的进程时,Console.WriteLine 不起作用 [英] Console.WriteLine does not work when attached to process in VS

查看:111
本文介绍了附加到 VS 中的进程时,Console.WriteLine 不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我在 C# .NET (winforms) 中有一个代码,其中包含一个 Console.WriteLine("TEST"); 并且 Visual Studio 附加到进程它不会写TEST"在输出窗口中.(申请已正确附加)

If I have a code in C# .NET (winforms) which contains a Console.WriteLine("TEST"); and visual studio is attached to the process it doesn't write "TEST" in the output window. (application is correctly attached)

如果我用 Visual Studio 正常启动我的程序,它就可以工作.

If I start my program normal with visual studio, it works.

为什么在第一种情况下不起作用?

推荐答案

Visual Studio,在调试 windows 程序时 (/target:winexe) 将使用 stdoutstderr 重定向到命名管道.命名管道的另一端归 VS 调试器所有,在这些管道上读取的任何内容(即应用程序写入 stdout 或 stderr 的任何内容)都将显示在 Debug 输出窗口中(由不是调试后的应用程序输出窗口).

Visual Studio, when debugging windows programs (/target:winexe) will launch them with the stdout and stderr redirected to Named Pipes. The other end of the named pipe is owned by the VS debugger and anything read on these pipes (ie. anything written by the app to stdout or stderr) will be displayed in the Debug Output window (which by no means is the debugged application Output window).

当附加到应用程序时,无法再进行此重定向(显然,因为应用程序已经启动并且 stderrstdin 无法在事后重定向).所以 Console.Write 不再自动神奇地重定向到调试输出.

When attach to an application, this redirect can no longer be done (obviously, since the application is already started and stderr and stdin cannot be redirected post-factum). So the Console.Write is no longer auto-magically redirected to the Debug output.

请注意,控制台程序不会发生此 stdout/stderr 重定向 (/target:exe)

Note that this stdout/stderr redircetion does not occur for console programs (/target:exe)

但是有一个专门的 API 来编写调试信息:Debug.Write.这使用 OutputDebugString 函数,将文本发送到附加的调试器(如果有).无论何时或如何附加调试器,这都有效,因为它不依赖于 stdout/stderr 重定向技巧.

But there is a dedicated API to write Debug info: Debug.Write. This uses the OutputDebugString functions, which sends text to the attached debugger (if any). This works no matter when or how the debugger is attached, as is not dependent upon stdout/stderr redirection tricks.

将您的 Console.Write 替换为 Debug.Write.

这篇关于附加到 VS 中的进程时,Console.WriteLine 不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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