c# - 如何以编程方式在与父进程相同的 Visual Studio 调试 session 中启动子进程?

标签 c# .net visual-studio debugging

在调试器下运行一个进程时,我想在同一个调试器中启动一个子进程。

目前,我在用

Process.Start("sample.exe");

我希望它是这样的:

if (Debugger.IsAttached)
    // start "sample.exe" in the same debugging session
else
    Process.Start("sample.exe");

我可以向子进程传递一个标志,指示它调用 Debugger.Launch(),但这不会捕获启动错误,它会导致调试 session ,其中一些功能未启用(例如编辑和继续等)。最好让调试器直接启动进程。

最佳答案

您应该将调试器附加到您正在启动的进程。这可以做到:

  1. 启动“sample.exe”后从 Visual Studio 手动选择它的菜单调试 > 附加到进程..
  2. 以编程方式将调试器附加到“sample.exe”中
  3. Attaching to a Process using VS.NET Automation Model
  4. 更新:您可以设置 Windows 环境以在每次“sample.exe”启动时附加调试器:Launch the Debugger Automatically (无论如何你都需要调用 Debugger.Break)
  5. 可能是一些外部工具

这是附加调试器的“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/

相关文章:

c# - 如何将数据库结果限制为只有一个?

c# - 转换 double 值以创建 Point 类型

c# - MiniProfiler ASP.NET Core - 基于用户角色的 ShouldProfile

c# - 如何知道鼠标光标是否在桌面屏幕上?

.net - 暗影 : Create an RSS Feed problems

c# - 在 Visual Studio 中创建 Windows Installer 项目

visual-studio - 拖动文件警告扩展 VS 2010?

c# - 比较两个数组

c# - 如何从模板-c#创建xml

c# - GAC 中 dll 的配置文件保存位置 - 最佳实践