Count the number of times a method executed RRS feed

  • Question

  • Hi
    I would like to count the number of times the following methods executed from application startup. How can I do that?
    System.Drawing.Graphics.FromHdcInternal(IntPtr hdc)
    System.Drawing.BufferedGraphicsContext.CreateBuffer(IntPtr src, Int32 offsetX, Int32 offsetY, Int32 width, Int32 height)
    Thursday, May 12, 2011 3:21 AM


  • You can use .NET Profiling APIs to monitor methods' enter/leave event:


    Receiving Method Entry and Exit Notifications

    One of the coolest features of the profiling API is the ability to be told when any managed method begins executions, and when it is about to return to its caller. This is possible because .NET methods need to be JITed before they're used. If you tell the profiling API that you're interested in method enters and leaves, it can communicate that fact to the JITer. The JITer, in turn, inserts calls at the beginning and end of the method to call special functions that you define.

    With a little effort, you can use the enter/leave capabilities to create a decent spy program that shows you every method as it executes. If you do this, you'll be astounded at how many methods are called just to start up a trivial .NET program. Because there's both enter and leave callbacks, you can see the nesting of method calls within other methods.

    It's important to note that the .NET runtime can make use of methods that have been pre-JITed. Unless you take special steps, these methods won't have the requisite callouts, so you won't see them with these callouts. In some cases, this can be fixed by monitoring JITCachedFunctionSearchStarted and returning FALSE in the pbUseCachedFunction parameter.


    More detail is available at: The .NET Profiling API and the DNProfiler Tool

    Eric Yang [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    • Marked as answer by eryang Monday, May 23, 2011 4:07 AM
    Tuesday, May 17, 2011 6:34 AM