views:

128

answers:

4

I would like to be able to skip over certain parts of my code when I set breakpoints. For example, I have a code block which iterates 52 times to create a deck of cards. This is working properly and I would rather not have to hit F11 to keep stepping through that block. Is there anyway for me to "skip" this so that the debugger just goes on to the next method call?

Language and IDE is C# in VS 2008.

+5  A: 

Do the following

  • Right click on the first line after the loop
  • Select "Run to Cursor"

This will do the equivalent of setting a breakpoint at that line, run the code as if you hit F5 and then delete the break point once it's hit.

Note: If the execution of the loop code hits another, real, breakpoint the execution will stop there and not where you clicked. If you think this may happen simply disable all breakpoints, do the trick above and re-enable all breakpoints.

JaredPar
Can you elaborate a bit more on how this is supposed to behave? I was trying it out and it seems like if I you try this in a code block with nested if's, while's, etc, it doesn't actually move to the cursor but the next if and/or while. I'm a bit noobish so it might just require a bit more "trial and error" on my part. :)
Pete
+5  A: 

Set another break point where you want to end up, and press F5 :)

Noon Silk
Interesting... so if I set two breakpoints I can skip all the code called between them by using the "Start Debugging" command while already in debug mode?
Pete
Indeed you can :) Though, when running in debug, `F5` is referred to as `Continue`.
Noon Silk
+2  A: 

I don't know of any way to skip a block, but you can put a breakpoint after the block or use run-to-cursor (ctrl-F10, I think).

Tal Pressman
+1  A: 

If it is an option, you could move that code into a function and apply the [DebuggerStepThrough] attribute to the function so that it will always be skipped during debugging

Like so:

using System.Diagnostics;

namespace MyNamespace
{
    public class Utilities
    {
     [DebuggerStepThrough]
     public ThisIsAVeryLongMethod()
     {
      //
      // Code
      //
     }
    }
}

Now in your code, when you step through, the debugger will not go to the method and just proceed to the next line

ShortMethod1();
ThisIsAVeryLongMethod();
ShortMethod2();
Conrad
I was going to ask but Google is my friend. I like this and it works the best I think for the specific purpose I need right now. I can't edit answers but someone should edit this to explain how to use the [DebuggerStepThrough] attribute and the fact that you need the System.Diagnostics namespace as well.
Pete