Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can not debug a Project started using Process.Start()

I have two C# WinForm projects in the same solution lets call them A and B. Project A starts Process B via a call like below

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Task.EXEFilename;
psi.WorkingDirectory = Path.GetDirectoryName(Data.EXEFilename);
Process.Start(psi);

The process B is started correctly. I wanted to debug the process B while I am debugging A. I thought putting a break point in the B would be enough but it is never hit. I have verified that process that is started is in the bin/debug folder of the B. I should not be doing attached to process in this case to switch debugging from A to B ?

like image 272
Ahmed Avatar asked Aug 29 '13 09:08

Ahmed


People also ask

How do I start a process in debug mode?

Spawning a new process on the same computerIn Visual Studio, from the Tools menu, choose Launch Under Debugger. In the Launch Under Debugger dialog box, enter the path to the executable file. You can also enter arguments and a working directory. Click Launch.

How do I attach a process in Visual Studio 2017 for debugging?

In visual studio click Tools | Attach to process. Then select appropriate service. On later versions of Visual Studio it's under Debug | Attach to process (instead of under Tools menu).

What is start without debugging?

Start without debugging, is to emulate the exact processing of the code. It is one single process, which gives you only the output, and you might now know the value of the variables during the processing, with the exceptions of using Response.


3 Answers

In the 2nd project have it check its command line arguments, if it sees something like --debug was passed in as the first argument the 2nd program launches the debugger itself

private static void Main(string[] args)
{
    //If no debugger is attached and the argument --debug was passed launch the debugger
    if (args.Length == 1 && args[0] == "--debug" && Debugger.IsAttached == false)
        Debugger.Launch();

    //(snip) the rest of your program

}

When you do this you will get a dialog window that will allow you to choose to open a new copy of Visual Studio or just use your already open copy.

image


You can also put the child process in the Image File Execution Options registry key.

like image 95
Scott Chamberlain Avatar answered Oct 18 '22 22:10

Scott Chamberlain


It sounds like you want Visual Studio to automatically attach to any child processes that are created during debugging. Other debuggers like windbg have this behavior but unfortunately Visual Studio does not. There is a user voice item which is tracking this request though

  • http://connect.microsoft.com/VisualStudio/feedback/details/772727/when-a-child-process-is-spawned-debugger-does-not-attach-to-it

Short term though the best option is to do simply break at the point the child process is spawned and manually attach the debugger.

var proc = Process.Start(psi);
Debugger.Break();  
like image 29
JaredPar Avatar answered Oct 18 '22 22:10

JaredPar


Try getting the process ID from Program B, something like:

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Task.EXEFilename;
psi.WorkingDirectory = Path.GetDirectoryName(Data.EXEFilename);
var proc = Process.Start(psi);

Debug.WriteLine(proc.Id);

Then load your project up in another instance of Visual Studio and use Debug > Attach to process to attach to Program B.

like image 44
Sean Airey Avatar answered Oct 18 '22 23:10

Sean Airey