【问题标题】:Is it possible to execute some code when visual studio enters break mode?当visual studio进入中断模式时是否可以执行一些代码?
【发布时间】:2018-01-19 16:43:38
【问题描述】:

我有一个需要调试的大型进程,这些进程可能随时停止。我已将 Visual Studio 2017 配置为在任何抛出的异常处停止,即使它已被处理,因为我想查看导致异常的原因。我需要的是在发生这种情况时发出某种警报,这样我就可以让程序继续运行,然后在出现任何情况时提醒我。我发现的唯一一件事是当一个断点被击中时会发出警报声,但这可能不是一个断点,我需要的不仅仅是声音,我需要能够执行一些代码,这样我就可以制作我的手机发疯什么的。当调试器进入中断模式时,有什么方法可以触发代码?

提前致谢。

【问题讨论】:

    标签: debugging exception break mode


    【解决方案1】:

    它是,使用 VS 包。您需要在类的顶部添加此属性,以便代码在包启动时运行:

    [ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids80.SolutionExists)] ///Able to run code on solution startup
    

    添加这些类值变量:

    private DTE2 applicationObject;
    private BuildEvents buildEvents;
    private DebuggerEvents debugEvents;
    

    那么下面的代码就可以运行了:

    protected override void Initialize()
    {
                base.Initialize();
    
                applicationObject = (DTE2)GetService(typeof(DTE));
                buildEvents = applicationObject.Events.BuildEvents;
                debugEvents = applicationObject.Events.DebuggerEvents;
    
                SetupEventHandlers();
            }
    

    最后是我们“全部”等待的代码:

    private void SetupEventHandlers()
            {
                //buildEvents.OnBuildDone += (scope, action) =>
                //{
    
                //};
    
                debugEvents.OnEnterBreakMode += delegate (dbgEventReason reason, ref dbgExecutionAction action)
                {
    
                };
    
                //var componentModel =
                //    GetGlobalService(typeof(SComponentModel)) as IComponentModel;
                //if (componentModel == null)
                //{
                //    Debug.WriteLine("componentModel is null");
                //    return;
                //}
                //var operationState = componentModel.GetService<IOperationState>();
                //operationState.StateChanged += OperationStateOnStateChanged;
            }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2010-11-24
      • 1970-01-01
      • 2018-03-25
      • 2021-12-02
      • 1970-01-01
      • 2010-12-24
      • 1970-01-01
      相关资源
      最近更新 更多