Stack Frame Inspection Sample

This sample demonstrates how to programmatically retrieve information about the call stack at any point from within the executing code. The stack allows you to determine the sequence of procedures that the application executed before getting to the current procedure.

To get samples and instructions for installing them

  • Do one or more of the following:

    • On the Help menu, click Samples.

      The Readme displays information about samples.

    • Visit the Visual Studio 2008 Samples Web site. The most recent versions of samples are available there.

    • Locate samples on the computer on which Visual Studio is installed. By default, samples and a Readme file are installed in drive:\Program Files\Microsoft Visual Studio 9.0\Samples\lcid. For Express editions of Visual Studio, all samples are located online.

For more information, see Visual Studio Samples.

Security noteSecurity Note:

This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.

To run this sample

  • Press F5.

Demonstrates

The .NET Framework provides the functionality as part of the StackFrame class. The sample demonstrates two other classes: StackTrace and MethodInfo. The parameterless constructor of StackTrace returns a complete set of StackFrame objects, providing information from the time your application started. You can also use the StackTrace constructor that takes an Exception instance. This constructor provides stack trace information back to the code that triggered the exception.

The event handler for the Test Procedure Stack button contains code to call several methods resulting in a stack several levels deep. Clicking the Test Procedure Stack button calls ProcA, which calls ProcB. ProcB calls the GetFullStackFrameInfo procedure, passing in a new StackTrace object:

GetFullStackFrameInfo(New StackTrace()) 

In the GetFullStackFrameInfo procedure, the code uses the FrameCount property of the StackTrace object to retrieve the total number of stack frames, working backwards from the current procedure. The procedure then adds information about each stack frame to the list box on the sample, calling the GetFrame method to retrieve an indexed stack frame object.

The GetStackFrameInfo procedure calls GetFrame on the StackFrame object the code sent to it, and then it retrieves properties of the MethodInfo object to display information about the procedure in the list box.

The event handler for the Test Exception Handling button generates an error down the stack several layers. The code triggers an exception and passes that exception to the constructor for the StackFrame object.

See Also

Reference

System.Diagnostics

StackFrame

System.Reflection

MethodInfo

FrameCount