2

Is there an option/attribute/... that prevents VS's debugger from stopping a debugging session inside a specific method? I'm asking because I'm suffering from the BSoD that the class Ping of .NET 4.0 sometimes triggers. See Blue screen when using Ping for more details.

private async Task<PingReply> PerformPing()
{
    // Do not stop debugging inside the using expression
    using (var ping = new Ping()) {
        return await ping.SendTaskAsync(IPAddress, PingTimeout);
    }
}
  • Of course I have some shutdown methods that cleanly stop the application. However, sometimes stopping the debugger mid-session isn't avoidable. –  Aug 31 '17 at 14:11
  • I think you need to clarify in your question that what you want is to stop the USER from hitting the STOP button while debugging and this method is executing. – JuanR Aug 31 '17 at 14:14
  • A user won't debug my program. That's just me. Sometimes I simply forget to shutdown the application or an exeption is thrown and I need to stop. –  Aug 31 '17 at 14:16
  • 1
    Precisely, when I say the user, I mean the debugging user and that is you. – JuanR Aug 31 '17 at 14:20
  • 1
    It is a silly bug, but it is pretty easy to avoid. Just don't ping when Debugger.IsAttached is true. – Hans Passant Aug 31 '17 at 15:18

2 Answers2

5

DebuggerStepthrough

Fun fact you can set it at a method level or a class level.

Instructs the debugger to step through the code instead of stepping into the code. This class cannot be inherited.

Tested with

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());
        }
    }
}

And the debugger didnt stop in the MAIN method

gh9
  • 10,169
  • 10
  • 63
  • 96
  • Does this actually prevent the debugger from stopping inside the method? –  Aug 31 '17 at 14:14
  • If you have a breakpoint inside this method you will miss it, and you cannot force a step into this method. Now does it stop the debugger from viewing an exception if thrown in the method? I do not know that, a simple enough test to find out though. – gh9 Aug 31 '17 at 14:16
  • 2
    It should be noted that this depends on your Just My Code setting. If you have Just My Code checked, this answer works as described. If not, this attribute is effectively ignored. –  Aug 31 '17 at 14:17
  • What he is trying to do is prevent the user of the debugger from stopping the debugging session while the Ping method is executing. – JuanR Aug 31 '17 at 14:21
  • @Juan I understand, putting debuggerStepthroug() should do that. – gh9 Aug 31 '17 at 14:23
  • @gh9 What I suspect he is experiencing is that the ping is executing asycn and the program continues to run (executing other code). He then stops the debugger somewhere else either because he needs to stop the debugging session or because an exception is encountered. If you read the link he provided, you will see that some things happen internally (locking) that cause the blue screen of death under these conditions. He doesn't want to step over, he wants to disable stopping anywhere in the app while Ping is running. – JuanR Aug 31 '17 at 14:29
  • @liondog yes, it prevents the debugger stopping inside the method. If an exception is thrown inside the method, the debugger will catch it at the caller. – Frank Hagenson Jul 03 '18 at 21:11
1

This answer ignores your BSoD and your Ping class, and focuses on the very interesting question of:

How to prevent the Visual Studio Debugger from stopping inside a specific method

(Note: this is "stopping", with an "o", not "stepping".)

So:

What seems to work nowadays is the [DebuggerHidden] attribute.

So, for example, consider the following method:

    ///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();
    }

If you then have the following:

Assert(false);

The debugger will stop on the Assert() invocation, not on the throw statement.

Mike Nakis
  • 56,297
  • 11
  • 110
  • 142