A week or so ago, I posted on using Visual Studio’s Watch window to modify an application’s user interface. This week, I’d like to show you another of my favorite Visual Studio debugger features: Run to Cursor.
Once again, I will use the NetCF WebCrawler sample as my debuggee and Visual Studio .NET 2003 as my debugger.
By using Run to Cursor, you can set a “one time”, or as John Robbins calls them “one shot” breakpoints. This allows you to break at a specific location exactly one time (especially nice when debugging a loop). To use, right-click on a line of code and select Run to Cursor from the context menu. The debugger will internally set a breakpoint for you and remove it when your application stops at it.
My favorite use for this feature is when first starting an application. Instead of having to set a breakpoint and press F5, I select Run to Cursor and my application is compiled, deployed and started.
This great feature even works for event handlers… In a Windows Forms application, select Run to Cursor in a button click handler, for example, and stop the first time the button is clicked.
To revisit last week’s tip, it can now be done as follows:
* Scroll to the first line in the startButton_Click method (MainForm.cs)
* Right-click and select Run to Cursor
Note that there is no breakpoint icon in the Indicator Margin (far left side of the code window)
* When the WebCrawler starts, click the Start button, Visual Studio stops in startButton_Click
We have run to our “one time” breakpoint
* Add addressBox.Text to the Watch window
* Change the value to your desired URL
* F5 to continue the application
In the earlier version of this tip, clicking the Start button again (who’s text gets changed to Stop, as part of the startButton_Click method) would cause the debugger to stop at the breakpoint we had previously set. With this new version, the breakpoint is managed by Visual Studio and is removed when we stop at it. Now when we stop the WebCrawler, we do not stop in the debugger.
[Edit: fix formatting]
This posting is provided “AS IS” with no warranties, and confers no rights.