如何以编程方式在与父项相同的Visual Studio调试会话中启动子进程?

Roh*_*hit 21 .net c# debugging visual-studio

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

目前,我用

Process.Start("sample.exe");
Run Code Online (Sandbox Code Playgroud)

我希望它是这样的:

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

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

Ser*_*kiy 25

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

  1. 启动"sample.exe"后从Visual Studio手动选择它菜单Debug> Attach to process ..
  2. 以编程方式将调试器附加到"sample.exe"中
  3. 使用VS.NET自动化模型附加到进程
  4. 更新:您可以设置Windows环境以在每次"sample.exe"启动时附加调试器:自动启动调试器(无论如何都需要调用Debugger.Break)
  5. 一些外部工具可能

以下是附加调试器的"sample.exe"代码:

if (!Debugger.IsAttached)
     Debugger.Launch();
Debugger.Break();
Run Code Online (Sandbox Code Playgroud)

您应该将一些参数传递给"sample.exe"以验证是否需要附加调试器.

Process.Start("sample.exe", "Debug=true");
Run Code Online (Sandbox Code Playgroud)

  • @lazyberezovsky但这需要开启新的VS. 如果子进程项目已存在于同一解决方案中,我们如何在同一个Visual Studio实例中执行此操作?谢谢 (3认同)
  • 它发射另一个VS. 是否可以在VS的相同实例中使用它. (2认同)