How to start a child process in the same Visual St

2020-02-09 00:51发布

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.

4条回答
女痞
2楼-- · 2020-02-09 01:29

Assuming that sample.exe would be the host for code you want to debug, you can use project properties -> debug settings - choose action as an external program and provide path to sample.exe. The VS will start this executable and attach the debugger to it.

查看更多
啃猪蹄的小仙女
3楼-- · 2020-02-09 01:42

you can change the properties of your solution to start multiple apps.

an explanation is here Run Multiple projects

The MSDN article is here MSDN Article on running multiple projects

查看更多
一纸荒年 Trace。
4楼-- · 2020-02-09 01:50

Visual Studio Debugger Team created an extension that does that automagically: https://marketplace.visualstudio.com/items?itemName=vsdbgplat.MicrosoftChildProcessDebuggingPowerTool

查看更多
贪生不怕死
5楼-- · 2020-02-09 01:54

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");
查看更多
登录 后发表回答