Run to Cursor for Easy Debugging in Visual Studio
This post is for Visual Studio, to learn how to do this in Visual Studio Code, go here. https://blueprogrammer.com/2021/03/27/run-to-cursor-for-easy-debugging-in-visual-studio-code/
I recently discovered the “Run To Cursor” command for launching the debugger in Visual Studio 2019. This will command will start the debugger and the program will execute normally until execution reaches the position of the cursor. Once this happens, break mode is triggered and the state is the same as if a breakpoint had been added at the same position.
This is very useful if you are trying to step through a programThe to find where things are going wrong. Adding breakpoints everywhere can be a bit cumbersome, as I often forget to remove them so the next time I run the program I find am hitting break points where they are not needed, slowing me down.
Starting the Debugger with “Run To Cursor”
To Start the debugger and execute the program to the current cursor position, right click on the line of code where you want the program to break, and select “Run To Cursor” from the context menu.
The CTRL + F10 shortcut is another way of accessing this command.
“Run To Cursor” with Visual Studio Code
In Visual Studio Code, the function is a bit different. There does not seem to be a simple way to start the debugger and run to the current line with a single command. However, when the debugger has already been started and a break point has been hit, the Run-To-Cursor option in the context menu does appear.
It is annoying to have to set a breakpoint first in order to use this function. A work-arround to avoid setting a break point in order to get access to the “Run to Cursor” function in Visual Studio Code is to edit the Launch.Json for the current project to change the debug configuration to stop and program entry.
When the “stopAtEntry” property is set to true, it is like setting a breakpoint at the first line of your program. The debugger will immediately go into break mode. In some cases, this may be easier than manually setting a breakpoint. Happy Debugging.