21

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.

Drew Noakes
  • 300,895
  • 165
  • 679
  • 742
Rohit
  • 3,610
  • 7
  • 45
  • 76
  • 1
    I suspect the title of this question is very misleading - are you trying to start this process with VS, or with a C# application that happens to be built using VS? – Will Dean Jan 27 '11 at 09:07
  • What you're asking doesn't make any sense. – Cody Gray - on strike Jan 27 '11 at 09:09
  • 2
    The problem is that Visual Studio won't automatically attach to child processes out of the box. There are solutions for that though. – Johannes Rudolph Jan 27 '11 at 09:13
  • Will: neither. am trying to start a process from a application built in c# but running in debug mode in VS. So in a way both your statements is valid. – Rohit Jan 27 '11 at 09:45
  • should I close/delete this question as it is dup of http://stackoverflow.com/questions/986470/start-process-and-attach-debugger – Rohit Jan 27 '11 at 09:50
  • We don't delete dupes; people search, find this one, and then are led to the answer. Its like a signpost pointing to Winsville. –  Feb 02 '11 at 11:57
  • 1
    A simpler solution is to call Main in sample.exe's project. – Henry Aloni Oct 13 '12 at 16:51
  • @HenryAloni The problem is that we need multiple STA threads for 2 winform programs and c# has many values that are in static properties. – Rohit Oct 14 '12 at 12:08
  • This is not a dupe of http://stackoverflow.com/questions/986470/how-do-i-attach-a-process-to-the-debugger-in-visual-studio -- that question talks about attaching a debugger manually. The OP here is asking about doing so programmatically from a debuggee that's already running under the debugger. – Drew Noakes Feb 26 '16 at 11:46

4 Answers4

25

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");
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459
  • 2
    It launches another VS. Is it possible to have it in the same instance of VS. – Rohit Jan 27 '11 at 09:43
  • Well, never thought about this. Launching another VS is a default behavior. BTW I found good walkaround for starting process in debug mode without modifying it. – Sergey Berezovskiy Jan 27 '11 at 10:27
  • 3
    @lazyberezovsky but this requires opening new VS. How can we do it in same Visual Studio instance if child process project is already existing within same solution? Thanks – AFgone Dec 14 '13 at 12:20
  • If the launched program happens to be the same as the launcher, VS10 (at least) doesn't open a new instance. In that case step one is the easiest , but in choosing step two, with the program as a debug compile, obviously `"Debug=true"` isn't required. – Laurie Stearn Feb 11 '16 at 13:36
1

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

Kev Hunter
  • 2,565
  • 4
  • 25
  • 39
  • This is a good solution if all processes start at the same time. However if a child process can start at some later time, it won't work. – Drew Noakes Feb 26 '16 at 11:48
0

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.

VinayC
  • 47,395
  • 5
  • 59
  • 72
0

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

Jacob
  • 627
  • 6
  • 15