代码之家  ›  专栏  ›  技术社区  ›  dss539

使用宏或键盘快捷键切换“引发异常时中断”

  •  42
  • dss539  · 技术社区  · 17 年前

    编辑:VisualStudio2015的新异常窗口比 我不再关心如何使用键盘的旧对话框 捷径。

    使用ctrl+alt+e打开对话框,选中“公共语言运行时异常”“抛出”框,然后单击“确定”非常简单,但这是我经常做的事情。我宁愿有一个键盘快捷键。

    这个问题是重复的 Any have a Visual Studio shortcut/macro for toggling break on handled/unhandled exceptions?

    工作

    “那么,我们写一个循环。”你说。但不要那么快! Someone tried that already 而且速度慢得毫无用处。(是的,我已经验证了它在我的系统上也很慢。)

    因此,挑战在于使用宏在1或2秒内切换整个CLR异常类别。 是否有Visual Studio快捷方式/宏用于切换已处理/未处理异常的中断?

    10 回复  |  直到 9 年前
        1
  •  18
  •   Bryce Kahle    17 年前

    Dim dbg As EnvDTE90.Debugger3 = DTE.Debugger
    Dim exSettings As EnvDTE90.ExceptionSettings = dbg.ExceptionGroups.Item("Common Language Runtime Exceptions")
    Dim exSetting As EnvDTE90.ExceptionSetting
    Try
        exSetting = exSettings.Item("Common Language Runtime Exceptions")
    Catch ex As COMException
        If ex.ErrorCode = -2147352565 Then
            exSetting = exSettings.NewException("Common Language Runtime Exceptions", 0)
        End If
    End Try
    
    If exSetting.BreakWhenThrown Then
        exSettings.SetBreakWhenThrown(False, exSetting)
    Else
        exSettings.SetBreakWhenThrown(True, exSetting)
    End If
    
        2
  •  14
  •   Andrey Shchekin    13 年前

    我已经创建了一个免费的Visual Studio扩展,可以可靠地做到这一点: Exception Breaker .
    IDebugSession2.SetException 调用速度非常快:所有异常都在20到60毫秒内设置/取消设置。

        3
  •  4
  •   Peter MacMurchy    16 年前

    以下是布莱斯·卡勒(Bryce Kahle)的非常有用的宏,该宏被盲目更新以在VS2010中运行:

    Sub ToggleExceptions()
        Dim dbg As EnvDTE100.Debugger5 = DTE.Debugger
        Dim exSettings As ExceptionSettings = dbg.ExceptionGroups.Item("Common Language Runtime Exceptions")
        Dim exSetting As ExceptionSetting
        Try
            exSetting = exSettings.Item("Common Language Runtime Exceptions")
        Catch ex As COMException
            If ex.ErrorCode = -2147352565 Then
                exSetting = exSettings.NewException("Common Language Runtime Exceptions", 0)
            End If
        End Try
    
        If exSetting.BreakWhenThrown Then
            exSettings.SetBreakWhenThrown(False, exSetting)
        Else
            exSettings.SetBreakWhenThrown(True, exSetting)
        End If
    
    End Sub
    
        4
  •  2
  •   xks    14 年前

    当模式对话框打开时,计时器开始计时。 如果您使用Win 7和停用的UAC SendKeys以及ALT Key将失败…我不知道为什么。

    我玩了一点…试试这个(VS2010 EN):

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports EnvDTE90
    Imports EnvDTE90a
    Imports EnvDTE100
    Imports System.Diagnostics
    Imports System.Runtime.InteropServices
    
    '...
    
    Private WithEvents t As Timers.Timer
    Private Sub t_Elapsed(ByVal ee As Object, ByVal dd As Timers.ElapsedEventArgs) Handles t.Elapsed
        t.Stop()
        ' Tastatureingaben simulieren
        System.Windows.Forms.SendKeys.SendWait("{DOWN}")
        System.Threading.Thread.Sleep(1500) ' Pause wichtig zum Laden des Exceptionbaums
        System.Windows.Forms.SendKeys.SendWait("%t")
        System.Windows.Forms.SendKeys.SendWait("{ENTER}")
    End Sub
    Public Sub toggleCLRExceptions()
        If DTE.Solution.Count <= 0 Then
            MsgBox("Nicht ohne geöffnete Solution!")
            Exit Sub
        End If
        ' Timer wird benötigt, da der Dialog Modal ist
        ' und weitere Befehle im Macro werden erst nach beenden des Dialogs ausgeführt
        t = New Timers.Timer()
        t.Interval = 0.5
        t.Start()
        DTE.ExecuteCommand("Debug.Exceptions")
        'System.Windows.Forms.SendKeys.SendWait("^%e") ' alternativ: STRG+ALT+e
        System.Threading.Thread.Sleep(200)
        If isCLRExceptionsActive() Then
            MsgBox("BREAK @CLR-Exception", MsgBoxStyle.Information, "Info")
        Else
            MsgBox("NO BREAK @CLR-Exception", MsgBoxStyle.Information, "Info")
        End If
    End Sub
    
    Function isCLRExceptionsActive() As Boolean
        ' prüft, ob Setting Debug CLR-Exceptions aktiviert/deaktivert ist
        Dim dbg As EnvDTE100.Debugger5 = DTE.Debugger
        Dim exSettings As ExceptionSettings = dbg.ExceptionGroups.Item("Common Language Runtime Exceptions")
        Dim exSetting As ExceptionSetting
        Try
            exSetting = exSettings.Item("Common Language Runtime Exceptions")
        Catch ex As COMException
            If ex.ErrorCode = -2147352565 Then
                exSetting = exSettings.NewException("Common Language Runtime Exceptions", 0)
            End If
        End Try
        Return exSetting.BreakWhenThrown
    End Function
    
    '...
    
        5
  •  1
  •   Simeon Pilgrim    17 年前

    我编写了一个基于VS2008 C的插件,可以切换386个异常,每个状态切换大约需要1秒。我想这是因为COM的交互作用。

    这是基于其中一个链接中的VB/宏代码。我找不到更容易的C++方法(但不能排除它)。

    下一个层次是制作一个具有键盘绑定的插件,然后打开Exceptions UI,然后为您“单击”正确的勾选框。

    祝你好运

        6
  •  1
  •   jsnfwlr    17 年前

    你可以使用像这样的工具 AutoHotKey 要创建录制的脚本(鼠标单击或按键),然后为其指定热键,以便在按下时播放脚本。。。

        7
  •  1
  •   Steve Dignan    17 年前

    只是提供一些我在这上面找到的信息( here

    其他人提出了同样的问题,支持部的Gary Chang对此进行了回答,以下是引用的回答:

    恐怕宏代码不能 操作上的操作

    需要注意的是,这篇帖子是2005年12月发布的,因此回复可能不再准确;不管怎样,我想我会把它扔出去。

        8
  •  1
  •   Ethan    16 年前

    为组设置特殊例外设置的建议确实会切换顶级复选框的状态。但是,它似乎不会在树中切换它下面的各个异常,而且,当抛出这些异常时,我的进程不会停止,就像我手动选中顶级复选框时那样。你看到不同的行为了吗?

        9
  •  1
  •   Igor Kostomin    14 年前

    我的宏在运行时忽略当前CLR异常。它的工作原理类似于调试时弹出异常时的“禁用捕获此异常类型”按钮。

    Imports System
    Imports EnvDTE
    Imports EnvDTE80
    Imports EnvDTE90
    Imports EnvDTE90a
    Imports EnvDTE100
    Imports System.Diagnostics
    Imports Microsoft.VisualBasic
    Imports Microsoft.VisualBasic.ControlChars
    
    ' execute Macros.MyMacros.VSDebuggerExceptions.IgnoreCurrentExceptionWhenThrown from VS Command Window
    
    Public Module VSDebuggerExceptions
    
        Sub BreakWhenThrown(Optional ByVal strException As String = "")
            Dim dbg As Debugger3 = DTE.Debugger
            Dim eg As ExceptionSettings = _
                dbg.ExceptionGroups.Item("Common Language Runtime Exceptions")
            eg.SetBreakWhenThrown(True, eg.Item(strException))
        End Sub
    
        ' copied from Utilities module (samples)
        Function GetOutputWindowPane(ByVal Name As String, Optional ByVal show As Boolean = True) As OutputWindowPane
            Dim window As Window
            Dim outputWindow As OutputWindow
            Dim outputWindowPane As OutputWindowPane
    
            window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
            If show Then window.Visible = True
            outputWindow = window.Object
            Try
                outputWindowPane = outputWindow.OutputWindowPanes.Item(Name)
            Catch e As System.Exception
                outputWindowPane = outputWindow.OutputWindowPanes.Add(Name)
            End Try
            outputWindowPane.Activate()
            Return outputWindowPane
        End Function
    
        Private WithEvents t As Timers.Timer
    
        ' Adds the current exception to ignore list
        Sub IgnoreCurrentExceptionWhenThrown()
            Dim commandWin As EnvDTE.CommandWindow
            commandWin = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindCommandWindow).Object
    
            Select Case DTE.Debugger.CurrentMode
                Case dbgDebugMode.dbgDesignMode
                    commandWin.OutputString("This macro is not enabled in Design Mode. Run it in Break Mode." + vbCrLf)
                    Return
    
                Case dbgDebugMode.dbgRunMode
                    commandWin.OutputString("This macro is not enabled in Run Mode. Run it in Break Mode." + vbCrLf)
                    Return
            End Select
    
            commandWin.OutputString(Environment.NewLine)
            commandWin.OutputString("Trying to get the information about current exception.." + Environment.NewLine)
    
            Dim dbg As Debugger3 = DTE.Debugger
            Dim currentExpression As Expression = dbg.GetExpression("$exception", False)
            Try    
                Dim currentExceptionTypeString As String = currentExpression.DataMembers.Item(1).Type
                commandWin.OutputString("Detected current exception type is : " + currentExceptionTypeString + Environment.NewLine)
    
                Dim flag As Boolean = True
                Dim eg As ExceptionSettings = dbg.ExceptionGroups.Item("Common Language Runtime Exceptions")
                Try
                    eg.SetBreakWhenThrown(False, eg.Item(currentExceptionTypeString))
                Catch exc As Exception
                    commandWin.OutputString("Cannot find this exception, trying to create.." + currentExceptionTypeString + Environment.NewLine)
                    '
                    eg.NewException(currentExceptionTypeString, New Random().Next)
                    eg.SetBreakWhenThrown(False, eg.Item(currentExceptionTypeString))
                    eg.SetBreakWhenUserUnhandled(True, eg.Item(currentExceptionTypeString))
                    flag = False
                End Try
    
                commandWin.OutputString(Environment.NewLine)
                commandWin.OutputString("Exception '" + currentExceptionTypeString + "' added to ignore list.")
                commandWin.OutputString(Environment.NewLine)
    
                t = New Timers.Timer()
                ' small interval to send keys after DTE will start to exec command
                t.Interval = 0.1
                t.Start()
                DTE.ExecuteCommand("Debug.Exceptions")
    
            Catch exc As Exception
                commandWin.OutputString("Error occured")
            End Try
        End Sub
    
        Private Sub t_Elapsed(ByVal ee As Object, ByVal dd As Timers.ElapsedEventArgs) Handles t.Elapsed
            t.Stop()
            ' only press Ok to apply changed exceptions settings to debugger
            System.Windows.Forms.SendKeys.SendWait("%t")
            System.Windows.Forms.SendKeys.SendWait("{ENTER}")
        End Sub
    
    End Module
    
        10
  •  1
  •   David Robinson    12 年前

    ALT+T