阻止VS Debugger在特定方法内停止

时间:2017-08-31 14:10:06

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

是否有一个选项/属性/ ...阻止VS的调试器在特定方法中停止调试会话?我问,因为我在BSoD中遇到.NET 4.0的类Ping有时会触发。有关详细信息,请参阅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);
    }
}

2 个答案:

答案 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方法中停止

答案 1 :(得分:0)

此答案将忽略您的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语句上停止。

相关问题