Datasheet

The next option is one of the more interesting options if you need to carry out a debug session in a live
environment. You can create a breakpoint on the debug version of code and then add a fi lter that ensures
you are the only user to stop on that breakpoint. For example, if you are in an environment where multiple
people are working against the same executable, then you can add a breakpoint that won t affect the other
users of the application.
Similarly, instead of just stopping at a breakpoint, you can also have the breakpoint execute some other
code, possibly even a Visual Studio macro, when the given breakpoint is reached. These actions are rather
limited and are not frequently used, but in some situations this capability can be used to your advantage.
Note that breakpoints are saved when a solution is saved by the IDE. There is also a Breakpoints window,
which provides a common location for managing breakpoints that you may have set across several different
source fi les.
Finally, at some point you are going to want to debug a process that isn t being started from Visual Studio for
example, if you have an existing website that is hosting a DLL you are interested in debugging. In this case,
you can leverage Visual Studio s capability to attach to a running process and debug that DLL. At or near
the top (depending on your settings) of the Tools menu in Visual Studio is the Attach to Process option. This
menu option opens a dialog showing all of your processes. You could then select the process and have the
DLL project you want to debug loaded in Visual Studio. The next time your DLL is called by that process,
Visual Studio will recognize the call and hit a breakpoint set in your code.
Other Debug - Related Windows
As noted earlier, when you run an application in Debug mode, Visual Studio .NET 2010 can open a series of
windows related to debugging. Each of these windows provides a view of a limited set of the overall environment
in which your application is running. From these windows, it is possible to fi nd things such as the list of calls
(stack) used to get to the current line of code or the present value of all the variables currently available. Visual
Studio has a powerful debugger that is fully supported with IntelliSense, and these windows extend the debugger.
Output
Recall that the build process puts progress messages in this window. Similarly, your program can also
place messages in it. Several options for accessing this window are discussed in later chapters, but at the
simplest level the Console object echoes its output to this window during a debug session. For example,
the following line of code can be added to your sample application:
Console.WriteLine("This is printed in the Output Window")
This line of code will cause the string This is printed in the Output Window to appear in the Output
window when your application is running. You can verify this by adding this line in front of the command to
open the message box. Then, run your application and have the debugger stop on the line where the message
box is opened. If you check the contents of the Output window, you will fi nd that your string is displayed.
Anything written to the Output window is shown only while running a program from the environment. During
execution of the compiled module, no Output window is present, so nothing can be written to it. This is the
basic concept behind other objects such as
Debug and Trace , which are covered in more detail in Chapter 6.
Call Stack
The Call Stack window lists the procedures that are currently calling other procedures and waiting for their
return. The call stack represents the path through your code that leads to the currently executing command.
This can be a valuable tool when you are trying to determine what code is executing a line of code that you
didn t expect to execute.
Locals
The Locals window is used to monitor the value of all variables currently in scope. This is a fairly self -
explanatory window that shows a list of the current local variables, with the value next to each item. As in
previous versions of Visual Studio, this display enables you to examine the contents of objects and arrays via a
Enhancing a Sample Application
51
CH001.indd 51CH001.indd 51 4/5/10 11:57:01 AM4/5/10 11:57:01 AM