Robert Petz
Robert Petz

Reputation: 2764

WPF Pass the debugger on to a newly spawned process

I have an application that spawns a new process. During development of the application, I would like to attach a debugger to the new process. Currently I can do that through Visual Studios Attach to Process functionality, but it's a pain to go back and attach it manually every single time when I need to test a minor change to the code.

Is it possible to spawn a new process using Process.Start and then attach any debuggers that are currently attached to the current process onto the new process?

I.E.:

Start Process1 with debugging enabled
Process1 starts Process2
Process1 attaches the debugger to Process2

Upvotes: 1

Views: 575

Answers (2)

Robert Petz
Robert Petz

Reputation: 2764

As an FYI to anyone else looking at this, here is the code implementation of Thomas Levesque's (accepted) answer:

Code

Launching app:

Process.Start(/** Path to assembly **/, System.Diagnostics.Debugger.IsAttached ? "/Debug" : "");

Target app:

if (Environment.GetCommandLineArgs().Contains("/Debug"))
    System.Diagnostics.Debugger.Launch();

This will cause most instances of Visual Studio to prompt you with this when the new process starts:

enter image description here

This is normal...just hit 'Yes, debug [assembly name]'. The next window will prompt you to choose the debugger to attach to, which usually defaults to the debugger that was orignaly used to launch the starting app

Upvotes: 2

Thomas Levesque
Thomas Levesque

Reputation: 292465

Not a very clean solution, but you could do something like this:

  • In Process1, if you're running in Debug mode (Debugger.IsAttached), pass an argument to Process2 (e.g. process2.exe /debug)
  • In Process2, if the process is started with the /debug argument, attach the debugger from code (Debugger.Launch())

Upvotes: 4

Related Questions