如何在同一个Visual Studio调试会话中以编程方式启动子进程? [英] How to start a child process in the same Visual Studio debugging session as the parent, programmatically?

查看:116
本文介绍了如何在同一个Visual Studio调试会话中以编程方式启动子进程?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



目前,我使用

  Process.Start(sample.exe); 

我想要这样做:



如果(Debugger.IsAttached)
//在同一个调试会话中启动sample.exe
else
Process.Start(sample 。可执行程序);

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

解决方案

您应该附加调试器来处理您的启动。这可以完成:


  1. 从Visual Studio手动启动sample.exe后,选择该菜单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();

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

  Process.Start(sample.exe,Debug = true); 


When running a process under the debugger, I would like to start a child process in the same debugger.

Currently, I use

Process.Start("sample.exe");

I want it to be something like this:

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

I could pass a flag to the child process that instructs it to call Debugger.Launch(), but that won't catch start up errors, and it results in a debugging session where some features are not enabled (such as edit and continue, etc). It's preferable to have the debugger launch the process directly.

解决方案

You should attach debugger to process you are starting. This could be done:

  1. Manually from Visual Studio after starting "sample.exe" select it menu Debug > Attach to process..
  2. Programmatically attach debugger inside "sample.exe"
  3. Attaching to a Process using VS.NET Automation Model
  4. UPDATE: You can setup windows environment to attach debugger every time "sample.exe" starts: Launch the Debugger Automatically (you will need to call Debugger.Break anyway)
  5. Some external tool maybe

Here is code for "sample.exe" to attach debugger:

if (!Debugger.IsAttached)
     Debugger.Launch();
Debugger.Break();

You should pass some parameter to "sample.exe" to verify if you need to attach debugger.

Process.Start("sample.exe", "Debug=true");

这篇关于如何在同一个Visual Studio调试会话中以编程方式启动子进程?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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