This article demonstrates how to monitor performance of an application to report items as handles, memory or threads. It demonstrates how to create a status bar with the display of those performance items in real time using C# in .Net 2/3. One can see if the application is leaking any resources in those areas.

Why not have your application provide information to its running state such as handles, threads and the bytes being used. This article will show you how to create a Winform Application with a status bar where each label reports handles, threads and the bytes used.

Steps:

  1. Create a winform application.
  2. Add the Timer component to the form and set these properties:
    1. Timer: Change the Interval from 100 to 1000 so it ticks off every second.
    2. Timer: Set Enabled to True
    3. Create a event for the Tick event.
  3. Add three PerfomanceCounter Components to the form and name them pcPrivateBytes, pcHandles and pcThreadsBuild the application and run it. Then set the properties below:
    1. All: CategoryName = Process
    2. All: InstanceLifetime = Global
    3. All: MachineName = . (Yes a period)
    4. All: InstanceName = (Find the running application name in the drop down list, hence why we are running it.)
    5. pcHandles: CounterName = Handle Count
    6. pcThreads: CounterName = Thread Count
    7. pcPrivateBytes: CounterName = Private Bytes (Private bytes is only the bytes that the application allocates and not any extraneous bytes allocated by the system)
  4. Close the running application.
  5. Add a Status Strip to the application and create three status label areas named tsslHandles and tsslThreads, tsslPrivateBytes. Note the Private bytes is pretty dynamic, so placing it at the end is a good idea.
  6. In the timer tick event add this code which will get the current values of the performance moniter:
    tsslThread.Text  = string.Format("Threads: {0}", pcThreads.NextValue().ToString());
    tsslHandles.Text = string.Format("Handles: {0}", pcHandles.NextValue().ToString()); 
    
    // Divide the return of Private Bytes to something we will recognize.
    tsslPrivateBytes.Text = string.Format("Private Bytes: {0}", (pcPrivateBytes.NextValue() / 1000).ToString());
  7. Run the application and you should see this which gives you a running total of what your application is doing.

image

Share