无法调试使用Process.Start()启动的项目

Ahm*_*med 9 c# debugging breakpoints

我在同一个解决方案中有两个C#WinForm项目,我们称它们为A和B.项目A通过如下调用启动进程B.

ProcessStartInfo psi = new ProcessStartInfo();
psi.FileName = Task.EXEFilename;
psi.WorkingDirectory = Path.GetDirectoryName(Data.EXEFilename);
Process.Start(psi);
Run Code Online (Sandbox Code Playgroud)

进程B正确启动.我想在调试A的同时调试进程B.我想在B中设置一个断点就足够但是它永远不会被击中.我已经验证了启动的进程是在B的bin/debug文件夹中.在这种情况下我不应该附加到进程来将调试从A切换到B?

Sco*_*ain 25

在第二个项目中检查它的命令行参数,如果它看到像--debug第一个参数传入的那样第二个程序启动调试器本身

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

}
Run Code Online (Sandbox Code Playgroud)

执行此操作时,您将看到一个对话框窗口,允许您选择打开Visual Studio的新副本或仅使用已打开的副本.

图片


您还可以将子进程放在Image File Execution Options注册表项中.


Jar*_*Par 6

听起来您希望Visual Studio自动附加到调试期间创建的任何子进程.像windbg这样的其他调试器有这种行为,但不幸的是Visual Studio没有.有一个用户语音项目正在跟踪此请求

短期虽然最好的选择是在生成子进程并手动附加调试器时简单地中断.

var proc = Process.Start(psi);
Debugger.Break();  
Run Code Online (Sandbox Code Playgroud)


Sea*_*rey 1

尝试从程序 B 获取进程 ID,例如:

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

Debug.WriteLine(proc.Id);
Run Code Online (Sandbox Code Playgroud)

然后在 Visual Studio 的另一个实例中加载您的项目,并使用“调试”>“附加到进程”来附加到程序 B。