Cannot debug a project started using Process.Start ()

I have two C # WinForm projects in the same solution that let you call them A and B. Project A starts process B when called, as shown below

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

Process B starts correctly. I wanted to debug process B while I debug A. I thought that putting a breakpoint in B would be enough, but it never gets there. I checked that the process started is in the bin / debug folder of B. Should I not connect to the process in this case to switch debugging from A to B?

+9
source share
5 answers

In the second project, check its command line arguments, if it sees something like --debug as the first argument, the second program starts 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 box that allows you to select a new copy of Visual Studio or simply use an already open copy.

image


You can also place the child process in the section "Image file image settings" .

+23
source

It looks like you want Visual Studio to automatically connect to any child processes created during debugging. Other debuggers, such as windbg, have this behavior, but unfortunately, Visual Studio does not. A custom voice element keeps track of this request, although

Short-term, although the best option is to simply interrupt the work when the child process starts and manually connect the debugger.

 var proc = Process.Start(psi); Debugger.Break(); 
+6
source

Try to get the process ID from program B, for example:

 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 the project into another instance of Visual Studio and use Debug> Attach to process to join program B.

+1
source

Here is the solution that they showed me ...

  1. Add a message box to the project you want to debug in project B.

    MessageBox.Show ("pause");

  2. Add a breakpoint after the point after the message box code in Project B.

  3. Run the code until a message box appears. message box image

  4. Choose Debug> Join Process

  5. . In the window that appears, select the name of the project that you are running and want to execute it, Project B. Click OK.

  6. Click OK on the message box that was generated by the message box code. message box image

You should now be at the breakpoint that you set some time after the message box code in project B!

+1
source

You can also try not to start process B directly. Right-click the solution, select Properties | General Properties | To run. Set process A and process B to Start. This models the process B started by process A (comment out the start of process B by process A in the source code A). Now you can check the interprocess communication between A and B, as if B was running A.

0
source

Source: https://habr.com/ru/post/1499570/


All Articles