How to programmatically start the second project f

2020-04-21 07:41发布

I have C# VS 2012 solution with two projects: ProjectA and ProjectB. Both projects are console applications for the sake of simplicity.

ProjectA needs to be started first so it is set as the "Startup project" for the solution. When I run ProjectA with F5 under debugger there are some operations it needs to performs after which it should start ProjectB under the same debugger. So if I set breakpoint in ProjectB first line, debugger should break.

Is there any way I can do this? Currently I use Process.Start("path_to_projectB.exe") but that needs to be replaced with something like "Process.StartUnderSameDebugger("path_to_projectB.exe")". I want to use the same instance of VS debugger that started ProjectA for ProjectB.

Note that I don't want to use "Start multiple projects" option in Visual Studio. If I do this both console apps are started simultaneously (which is unwanted) and under the same debugger (which is wanted). First process must perform some stuff before it can start second process and that's why I can't use this approach.

Also when I later stop debugger both processes (console apps from ProjectA and ProjectB) should be closed.

3条回答
Deceive 欺骗
2楼-- · 2020-04-21 07:57

You can always manually right click-debug the second project after the first project has started- both will end when the debugger stops.

查看更多
霸刀☆藐视天下
3楼-- · 2020-04-21 08:12

The Visual Studio team has released a Visual Studio extension that allows automatically attaching child processes to the current debugger: Introducing the Child Process Debugging Power Tool.

So you can start your ProjectB from ProjectA with Process.Start and it will automatically attach the debugger to this second process.

It is available on the Gallery for Visual Studio 2013 and above.

You can also check my answer here for how to attach the current debugger to any process you may start. Note that it would not be as immediate as the Visual Studio extension since the debugger would be attached after the process has started.

查看更多
啃猪蹄的小仙女
4楼-- · 2020-04-21 08:16

These options

  1. Create a unit test which mimics the test scenario given.
  2. Create project C which either references the projects or includes the target CS files as linked files (Add Existing Item -> Add (select drop down add as link)) and replicate the process needed to be tested.
查看更多
登录 后发表回答