Breakpoint Properties

In the Global.asax.cs file, right-click the breakpoint line in the Application_BeginRequest code and select Breakpoint Properties from the context menu to view the Breakpoint Properties dialog (see Figure 9.5).

Figure 9.5: The Breakpoint Properties dialog

When you load the dialog with a breakpoint already selected, the File, Line, and Character fields are already filled in for you, but you should note that you can set them yourself. For example, if you receive an error message while running some code, you can create a new breakpoint and fill in the information that you obtained from the error message.

It's useful to explore this dialog's properties a little. First, you can set a condition. For example, suppose you want to break only when a variable has the value true. Click the Condition button to view the Breakpoint Condition dialog. Enter Application["Debug"] into the Condition field. Make sure the Condition check box and the radio button labeled Is True are selected (see Figure 9.6).

Figure 9.6: The Breakpoint Condition dialog

Close the dialog. Leave the breakpoint active in the Application_BeginRequest code and press F5 to run the application again. This time, it won't break on the line because the Application["Debug"] variable doesn't exist yet. Stop the application, load the ch9-1.aspx file into the design editor window, and add a button to the Web Form. Name the button btnDebug and give it a text value of "Debug". Double-click the new Debug button and enter the code in Listing 9.1 into the btnDebug_Click event.

Listing 9.1: Test a Breakpoint Condition (ch9-1.aspx.cs)

private void btnDebug Click(object sender, System.EventArgs e) { int debugCount;

if (Application["Debug"]== null) { Application["Debug"] = false;

if (Session["Debug"]==null) { Session["Debug"] = 0;

debugCount = (int) Session["Debug"]; Session["Debug"] = ++debugCount; if (debugCount % 2 == 0) { Application.Lock(); Application["Debug"] = true; Application.UnLock();

Application.Lock(); Application["Debug"] = false; Application.UnLock();

Response.Write("Session[Debug]=" + Session["Debug"] + "<br>"); Response.Write("Application[Debug]=" + Application["Debug"] + "<br>");

This rather long example creates an Application["Debug"] variable if it doesn't already exist and a Session["Debug"] variable and then switches the value of the Application["Debug"] variable between true and false each time you request the page by clicking the button, causing the breakpoint to fire every other time. Run the application to test it.

You can also cause a breakpoint to fire based on the number of times a line executes. Right-click the breakpoint line in the Application_BeginRequest code again and select Breakpoint Properties from the context menu to reopen the Breakpoint Properties dialog. Click the Condition button and delete the condition. Next, click the Hit Count button and select the item that reads Break When the Hit Count Is Equal To from the When the Breakpoint Is Hit drop-down list (see Figure 9.7).

Figure 9.7: The Breakpoint Hit Count dialog

Enter the number 3 into the text field to the right of the drop-down list. Click OK to close the Breakpoint Hit Count dialog, and then click OK to close the Breakpoint Properties dialog. Run the project again. This time, don't click the Debug button, just refresh the form. The second time you refresh it (the third time the browser requests the page), VS will break in the Global.asax.cs file in the

Application BeginRequest event code.

Was this article helpful?

0 0

Post a comment