Search code examples
c#debuggingmonoxamarin-studio

Debugger.Break() when already at breakpoint


I like to call Debugger.Break() on various error conditions. I find this is a more effective way to debug than is throwing an exception.

The problem happens when I am already broken in the debugger. If that's the case, the debugger understandably gives up and dies. I would greatly prefer to simply not break in that case.

Is there a way to determine whether the debugger is already breaking? Or, failing that, a way to tell the debugger to just ignore a break call if it is already breaking?


Solution

  • This seems to do it. I imagine one could get it to fail if one tried hard enough.

    public static bool AtBreakPoint() {
      Task<int> task = Task.Run(() => 2);
      for (int i = 0; i < 1000000; i++) {
        if (task.IsCompleted) {
          return false;
        }
      }
      return true;
    }