Debugging

When you're compiling code in the default Debug mode, the compiler includes several options that may be used in order to assist you during the process of debugging an application. These options include stepping through the code, using code breakpoints, using conditional debugging when an exception is thrown, using process monitoring, and analyzing the program state using the .NET debugging tools.

graphics/note_icon.gif

The .NET Framework supports transparent debugging of applications that use multiple languages. If some modules of your application are written in Visual Basic .NET and others are written in Visual C# .NET, the debugger can step between these languages transparently to you.


Stepping Through Code During Execution

One method used in debugging an application is the step-by-step execution of its code within the debugger. If the application terminates unexpectedly, you will have a good idea of where in the application the problem lies. You may also check for variable values and other ongoing changes being made by the code during its execution so that you may determine the effect each step has produced.

Visual Studio .NET supports three ways to step through an application while you're debugging it. You can use the keyboard shortcuts shown in Table 12.5 to select the action to perform. Note that the shortcuts shown are for the Visual Basic keyboard mapping; your copy of Visual Studio .NET may use a different mapping. You can check your keyboard mapping by selecting My Profile from the Visual Studio .NET Start Page.

Table 12.5. Options for Step-by-Step Application Execution

Debug Menu Option

Keyboard Shortcut

Function

Step Into

F8

This action allows for the step-by-step execution of program code. If the debugger encounters a method call, it will step into the code of the method.

Step Over

Shift+F8

This action also allows for the step-by-step execution of program code. If the debugger encounters a method call, the entire method will be treated as a single step.

Step Out

Ctrl+Shift+F8

After you have used the Step Into action to step into a method, you can use Step Out to execute the remaining lines of code in the method as a single step, returning to the calling code.

Using Breakpoints

A breakpoint causes code execution to pause when encountered . If you place a breakpoint in your code, the application will pause when it reaches that point, allowing you to do the following:

  • Examine the current program state of the application

  • Enter step-by-step mode

  • Continue program execution

Within the Visual Studio .NET IDE, you may add a breakpoint by right-clicking the beginning of a line of code and selecting Add Breakpoint from the options provided. The New Breakpoints dialog box includes options for creating a breakpoint within a file, function, or address within the code as well as when a specific condition is met.

You may right-click an existing breakpoint to remove or disable the breakpoint's operation. Removing a breakpoint also removes any information you have associated with it using the Breakpoints window, whereas disabling a breakpoint simply causes the debugger to ignore it and continue normally. You can open the Breakpoints window, shown in Figure 12.2, by selecting Debug, Windows , Breakpoints from the header.

Figure 12.2. The Breakpoints window showing a breakpoint set on the 32nd line of code within the Calculator application.

graphics/12fig02.jpg

Debugging When an Exception Is Thrown

You can configure the debugger to respond when a particular exception is thrown by using the Exceptions dialog box shown in Figure 12.3. Here, you can configure the debugger to continue or break execution when an exception is thrown or when an unhandled exception occurs, alerting you to the presence of an unhandled exception within your code.

Figure 12.3. The Exceptions dialog box allows you to decide whether to break into the debugger when an exception is thrown or if it is not handled.

graphics/12fig03.jpg

Debugging a Running Process

You can configure debugging for a running process on a local or remote system running the Machine Debug Manager ( mdm.exe ) service. This service is loaded with Visual Studio .NET or through the Remote Components Setup Wizard included with Visual Studio .NET. After instantiating the process, open the Processes dialog box through Debug, Processes or Tools, Debug Processes within the header of the Visual Studio IDE.

Select the target system and a running process within the Processes dialog box. Then click the Attach button. You are now able to attach the Common Language Runtime (CLR) to add the process to your selected " debugged processes." If you want to debug a DLL file rather than an EXE file, you will need to create a simple application that invokes the DLL and then attach to the invoking application's process.

Analyzing Program State

During the debugging process, you will want to be able to evaluate many conditions and values that changed while the program was running. The Visual Studio .NET development environment includes several windows that provide you with this information. You will find several useful windows in the Debug, Windows menu:

  • Autos window This window includes information on the variables in the current and previous statements.

  • Call Stack window This window displays the path through which program execution reaches the current point in the code.

  • Immediate window This window can be used to invoke real-time evaluation of variables and functions in the Command window.

  • Locals window This window shows all the variables that are local to the current method being executed.

  • Me window This window shows all the members associated with the current object.

  • Watch window Each Watch window can be used to monitor the value of a particular variable or expression.

graphics/alert_icon.gif

In earlier versions of Visual Studio, you could break the code's execution, change values as desired, and then continue the code's execution from that point using the new values. Due to the .NET Framework's Just-In-Time compilation method, this will not work within Visual Studio .NET. You can change code while it's being debugged, but the changes will not take effect until you stop and restart the application.




Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
Developing and Implementing WindowsR-based Applications with Visual BasicR. NET and Visual StudioR. NET Exam CramT 2 (Exam 70-306)
ISBN: N/A
EAN: N/A
Year: 2002
Pages: 188

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net