tags:

views:

1798

answers:

4

What is the best way to determine how many window handles an application is using? Is there a tool or a WMI performance counter that I could use?

I would like to run up an app and watch a counter of some sort and see that the number of window handles is increasing.

for (int i=0; i < 1000; i++)
{
    System.Threading.Thread.Sleep(1000);
    RichTextBox rt = new RichTextBox();
    rt.Text = "hi";
    this.Controls.Add(rt);
}

I am running the above code and watching the "Handle Count" counter on the process, and it does not seem to be increasing. Is there something I am looking at incorrectly?

+4  A: 

Perfmon, which comes with your computer can do it. You can also add a column to your task manager processes tab (Handle Count).

Instructions for Perfmon

  1. Add a counter (click the +)
  2. Choose Process under Performance object
  3. Choose Handle Count under the counter list
  4. Choose your process from the instance list
  5. Click Add, click Close

To get the graph in range, you have to right-click it in the list, choose properties, and then choose the right scale (.1 or .01 would probably be right)

Edit (in response to added information): I think you just proved that creating RichTextBoxes doesn't result in Handles being allocated. I don't think it really needs one until you are editing the control and it might be smart enough to do that, since allocating too many resources for a control that isn't active would make it hard to have a lot of controls on a form (think about Excel, for example).

Lou Franco
good point - do you have any thoughts on how produce this behavior of allocating handles?
adeel825
I'm not really sure what you are trying to do. I think calling GlobalAlloc will increase the handle count. Here is the declaration you need: http://network.programming-in.net/articles/art9-1.asp?f=GlobalAlloc
Lou Franco
+2  A: 

Process Monitor is very handy in interactively monitoring all sorts of resources used by Windows processes.

Process Monitor is an advanced monitoring tool for Windows that shows real-time file system, Registry and process/thread activity.

Note - if you mean finding the information programatically, .Net provides access to all performance counters. You use the System.Diagnostics.PerformanceCounter Class like this:

PerformanceCounter PC=new PerformanceCounter();
PC.CategoryName="Process";
PC.CounterName="Handles";
PC.InstanceName="MyProc";
MessageBox.Show(PC.NextValue().ToString());
gimel
A: 

The handle count shown by taskmanager is the same as the one shown by PerfMon

ProcessExplorer tool from sysinternals can list the different type of handles + their names a process uses and you can get a good idea by browsing that list about the composition of the handles your program uses.

But I'm afraid it does not sumarize these handle type counts for you.

To view the actual handles and their types using ProcessExplorer - View - show lower pane view - handles.

You can also use some sort window spy tool which shows all the windows in the system like Microsoft spy++ or Managed Spy++ (http://msdn.microsoft.com/en-us/magazine/cc163617.aspx)

This will allow you to see if your windows are being created.

A: 

Perfmon or Task Manager cannot give you the number of WINDOW handles used by a process only the total number of handles of all types (file, thread, etc.).

The best information that I can find on the subject is this post which indicates that the window handle count for a process can be determined by enumerating all child windows of the main process window.