在调试器下运行一个进程时,我想在同一个调试器中启动一个子进程。
目前,我在用
Process.Start("sample.exe");
我希望它是这样的:
if (Debugger.IsAttached)
// start "sample.exe" in the same debugging session
else
Process.Start("sample.exe");
我可以向子进程传递一个标志,指示它调用 Debugger.Launch()
,但这不会捕获启动错误,它会导致调试 session ,其中一些功能未启用(例如编辑和继续等)。最好让调试器直接启动进程。
最佳答案
您应该将调试器附加到您正在启动的进程。这可以做到:
- 启动“sample.exe”后从 Visual Studio 手动选择它的菜单调试 > 附加到进程..
- 以编程方式将调试器附加到“sample.exe”中
- Attaching to a Process using VS.NET Automation Model
- 更新:您可以设置 Windows 环境以在每次“sample.exe”启动时附加调试器:Launch the Debugger Automatically (无论如何你都需要调用 Debugger.Break)
- 可能是一些外部工具
这是附加调试器的“sample.exe”代码:
if (!Debugger.IsAttached)
Debugger.Launch();
Debugger.Break();
您应该将一些参数传递给“sample.exe”以验证您是否需要附加调试器。
Process.Start("sample.exe", "Debug=true");
关于c# - 如何以编程方式在与父进程相同的 Visual Studio 调试 session 中启动子进程?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4814361/