C# 调试期间在 Visual Studio 中自动附加到子进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/422768/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-04 02:49:44  来源:igfitidea点击:

Attaching to a child process automatically in Visual Studio during Debugging

c#.netvisual-studiodebuggingvisual-studio-debugging

提问by Sam Saffron

When writing plugins for media center your plugin is hosted in ehexthost.exethis exe gets launched from ehshell.exeand you have no way of launching it directly, instead you pass a special param to ehshell.exewhich will launch the plugin in a separate process.

当为媒体中心编写插件时,你的插件托管在ehexthost.exe这个 exe 中ehshell.exe,你无法直接启动它,而是传递一个特殊的参数ehshell.exe,它将在单独的进程中启动插件。

When we are debugging media browserI find the process of attaching to a second process kind of clunky, I know about Debugger.Attach and also of some special registryentries I can use.

当我们调试媒体浏览器时,我发现附加到第二个进程的过程很笨拙,我知道 Debugger.Attach 以及一些我可以使用的特殊注册表项。

Both these methods don't exactly fit my bill. What I want is to press F5 and have my current instance of visual studio attach to the child process automatically. Can this be done?

这两种方法都不完全符合我的要求。我想要的是按 F5 并让我当前的 Visual Studio 实例自动附加到子进程。这能做到吗?

If there is a plugin for VS that allows me to achieve this functionality I would be happy with it.

如果 VS 有一个插件可以让我实现这个功能,我会很高兴的。

EDIT

编辑

I ended up going with the following macro:

我最终使用了以下宏:

Public Sub CompileRunAndAttachToEhExtHost()

    DTE.Solution.SolutionBuild.Build(True)
    DTE.Solution.SolutionBuild.Debug()

    Dim trd As System.Threading.Thread = New System.Threading.Thread(AddressOf AttachToEhExtHost)
    trd.Start()

End Sub

Public Sub AttachToEhExtHost()
    Dim i As Integer = 0
    Do Until i = 50
        i = i + 1
        Try

            For Each proc As EnvDTE.Process In DTE.Debugger.LocalProcesses
                If (proc.Name.IndexOf("ehexthost.exe") <> -1) Then
                    proc.Attach()
                    Exit Sub
                End If
            Next
        Catch e As Exception
            ' dont care - stuff may be busy 
        End Try
        Threading.Thread.Sleep(100)
    Loop
End Sub

Also, I outlined the process on how to get this goingon my blog.

另外,我在我的博客上概述了如何实现这一点的过程。

采纳答案by Jab

I would use a macro. I've redefined my F5 function to attach to the asp.net process instead of the long build/validate it usually performs. This works pretty well for me and it's really easy.

我会使用宏。我重新定义了 F5 函数以附加到 asp.net 进程,而不是它通常执行的长时间构建/验证。这对我来说非常有效,而且非常简单。

    For Each process In DTE.Debugger.LocalProcesses
        If (process.Name.IndexOf("aspnet_wp.exe") <> -1) Then
            process.Attach()
            Exit Sub
        End If
    Next

回答by galets

You can automatically attach to a process by pressing F5, if you setup something like that in visual studio:

如果你在 Visual Studio 中设置了类似的东西,你可以通过按 F5 自动附加到一个进程:

http://vvcap.net/db/ujYL7zeN_n_RgeprqCSM.htp

http://vvcap.net/db/ujYL7zeN_n_RgeprqCSM.htp

notice: There's "Command" filled up as an executable name, and "Attach" must be "yes"

注意:“Command”填写为可执行名称,“Attach”必须为“yes”

回答by Pablo Retyk

Check out the VisualStudio plugin that I wrote, named Lazy.

查看我编写的 VisualStudio 插件,名为Lazy

回答by Mike Chamberlain

For VS2012, macros have been dropped, but you can still do it quite quickly with standard keyboard shortcuts. For instance, to attach to iisexpress.exe:

对于 VS2012,宏已被删除,但您仍然可以使用标准键盘快捷键快速完成。例如,要附加到 iisexpress.exe:

Ctrl+ Alt+ p- brings up the Attach To Process dialog

Ctrl+ Alt+ p- 弹出附加到进程对话框

i- jumps to the the first process beginning with i in the list (for me this is iisexpress.exe)

i- 跳转到列表中以 i 开头的第一个进程(对我来说这是 iisexpress.exe)

Enter- attaches

Enter- 附加

For super speed, you can also Turn off Visual Studio Attach security warning when debugging IIS.

为了超速,您还可以在调试 IIS 时关闭 Visual Studio 附加安全警告

回答by mstrange

I was debugging a C++ plugin in an externally spawned process that crashed by throwing an exception at startup and this worked perfectly for me:

我正在一个外部生成的进程中调试 C++ 插件,该进程因在启动时抛出异常而崩溃,这对我来说非常有效:

Add the free Reattach Extension for Visual Studio. Ask it to reattach to the process name before it is launched. It will pop a modal dialog saying it is waiting for the process name to launch.

添加免费的 Visual Studio 重新附加扩展。在启动之前要求它重新附加到进程名称。它会弹出一个模式对话框,说它正在等待进程名称启动。

Now launch the process and the Visual Studio debugger will attach immediately, catching exceptions and hitting breakpoints.

现在启动进程,Visual Studio 调试器将立即附加,捕获异常并命中断点。

(This was also in a media plugin, the exception was normally caught and rethrown by the host process in a Delphi context so I needed to break before that happened).

(这也是在媒体插件中,异常通常由 Delphi 上下文中的宿主进程捕获并重新抛出,因此我需要在发生之前中断)。