【问题标题】:Prevent VS Debugger from stopping inside a specific method防止 VS 调试器在特定方法内停止
【发布时间】:2017-08-31 14:10:05
【问题描述】:

是否有一个选项/属性/...可以防止 VS 的调试器在特定方法中停止调试会话?我之所以问,是因为我患有 .NET 4.0 的 Ping 类有时会触发的 BSoD。详情请见Blue screen when using Ping

private async Task<PingReply> PerformPing()
{
    // Do not stop debugging inside the using expression
    using (var ping = new Ping()) {
        return await ping.SendTaskAsync(IPAddress, PingTimeout);
    }
}

【问题讨论】:

  • 当然,我有一些关闭方法可以彻底停止应用程序。但是,有时在会话中停止调试器是不可避免的。
  • 我认为你需要在你的问题中澄清你想要的是阻止用户在调试时点击停止按钮并且这个方法正在执行。
  • 用户不会调试我的程序。这就是我。有时我只是忘记关闭应用程序或抛出异常,我需要停止。
  • 准确地说,当我说用户时,我是指调试用户,就是你。
  • 这是一个愚蠢的错误,但很容易避免。只是当 Debugger.IsAttached 为真时不要 ping。

标签: c# visual-studio-2013 .net-4.0


【解决方案1】:

DebuggerStepthrough

有趣的是,您可以在方法级别或类级别设置它。

指示调试器单步执行代码,而不是单步执行代码。这个类不能被继承。

经过测试

using System;
using System.Diagnostics;

public class Program
{
    [DebuggerStepThrough()]
    public static void Main()
    {
        try
        {
            throw new ApplicationException("test");
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.ToString());
        }
    }
}

并且调试器并没有在 MAIN 方法中停止

【讨论】:

  • 这真的会阻止调试器在方法内部停止吗?
  • 如果你在这个方法中有一个断点你会错过它,你不能强行进入这个方法。现在,如果在方法中抛出异常,它会阻止调试器查看异常吗?我不知道,一个足够简单的测试来找出答案。
  • 需要注意的是,这取决于您的 Just My Code 设置。如果您选中了“仅我的代码”,则此答案按描述工作。如果不是,则该属性将被有效地忽略。
  • 他试图做的是阻止调试器的用户在执行 Ping 方法时停止调试会话。
  • @Juan 我明白,放置 debuggerStepthroug() 应该这样做。
【解决方案2】:

这个答案忽略了你的 BSoD 和你的 Ping 类,并专注于非常有趣的问题:

如何防止 Visual Studio 调试器在特定方法内停止

(注意:这是“停止”,带有“o”,而不是“步进”。)

所以:

现在似乎起作用的是 [DebuggerHidden] 属性。

因此,例如,考虑以下方法:

    ///An assertion method that does the only thing that an assertion method is supposed to
    ///do, which is to throw an "Assertion Failed" exception.
    ///(Necessary because System.Diagnostics.Debug.Assert does a whole bunch of useless, 
    ///annoying, counter-productive stuff instead of just throwing an exception.)
    [DebuggerHidden] //this makes the debugger stop in the calling method instead of here.
    [Conditional("DEBUG")]
    public static void Assert(bool expression)
    {
        if (expression)
            return;
        throw new AssertionFailureException();
    }

如果你有以下情况:

Assert(false);

调试器将在Assert() 调用上停止,而不是在throw 语句上。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2022-08-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 1970-01-01
    相关资源
    最近更新 更多