Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
strauss_d_getting_started_with_visual_studio_2022_learning_a.pdf
Скачиваний:
123
Добавлен:
26.06.2023
Размер:
17.04 Mб
Скачать

Chapter 3 Debugging Your Code

you forced the debugger to, skipping all the breakpoints in between. This is especially convenient since I do not have to remove my breakpoints, nor do I have to keep on pressing F5 for each breakpoint hit.

Conditional Breakpoints and Actions

Sometimes, you need to use a condition to catch a bug. Let’s say that you are in a for loop, and the bug seems to be data related. The erroneous data only seems to enter the loop after several hundred iterations. If you set a regular breakpoint, you will be pressing F10 until your keyboard stops working.

This is a perfect use case for using conditional breakpoints. You can now tell the debugger to break when a specific condition is true. To set a conditional breakpoint, right-click the breakpoint, and click Conditions from the context menu as seen in Figure 3-9.

Figure 3-9.  Breakpoint context menu

You can now select a conditional expression and select to break if this condition is true or when changed as seen in Figure 3-10.

172

Chapter 3 Debugging Your Code

We will discuss Actions shortly.

Figure 3-10.  Conditional expression

You can also select to break when the Hit Count is equal to, a multiple of, or greater or equal to a value you set as seen in Figure 3-11.

Figure 3-11.  Hit Count condition

The last condition you can set on a conditional breakpoint is a Filter as seen in Figure 3-12.

173

Chapter 3 Debugging Your Code

Figure 3-12.  Filter condition

You will have noticed the Actions checkbox from the Breakpoint Settings. You will also see the Actions menu on the context menu in Figure 3-9. Here, you can add an expression to log to the Output Window using specific keywords that are accessed using the $ symbol.

The special keywords are as follows:

•\

$ADDRESS – Current instruction

•\

$CALLER – Previous function name

•\

$CALLSTACK – Call stack

•\

$FILEPOS – The current file and line position

•\

$FUNCTION – Current function name

•\

$PID – Process ID

•\

$PNAME – Process name

•\

$TICK – Milliseconds elapsed since the system was started, up to

 

49.7 days

•\

$TID – Thread ID

•\

$TNAME – Thread name

You can now use these special keywords to write an entry to the Output Window. You can include the value of a variable by placing it between curly braces (think of interpolated strings). Listing 3-3 shows an example of an expression that uses the $FUNCTION keyword.

174

Chapter 3 Debugging Your Code

Listing 3-3.  Action Expression

The value of the counter = {iCount} in $FUNCTION

Placing this breakpoint action in the constructor of the Login() form of the ShipmentLocator application will be indicated by a diamond instead of a circle as seen in Figure 3-13.

Figure 3-13.  Breakpoint action

When you run your application, you will see the expression output in the Output Window as seen in Figure 3-14.

Figure 3-14.  Action expression in Output Window

This is great for debugging because if you don’t select a condition, the Action will be displayed in the Output Window without hitting the breakpoint and pausing the code. The breakpoint action can be seen in Figure 3-15.

175