windows 如何确定应用程序正在使用的窗口句柄数?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/197497/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:27:02  来源:igfitidea点击:

How do I determine number of window handles an application is using?

windowswinapi

提问by adeel825

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?

确定应用程序正在使用多少个窗口句柄的最佳方法是什么?是否有我可以使用的工具或 WMI 性能计数器?

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?

我正在运行上面的代码并观察进程上的“句柄计数”计数器,它似乎没有增加。有什么我看错了吗?

回答by Lou Franco

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

计算机附带的 Perfmon 可以做到。您还可以向任务管理器进程选项卡(处理计数)添加一列。

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
  1. 添加计数器(单击 +)
  2. 在 Performance 对象下选择 Process
  3. 在计数器列表下选择处理计数
  4. 从实例列表中选择您的流程
  5. 点击添加,点击关闭

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)

要获得范围内的图形,您必须在列表中右键单击它,选择属性,然后选择正确的比例(0.1 或 0.01 可能是正确的)

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).

编辑(响应添加的信息):我认为您刚刚证明创建 RichTextBoxes 不会导致分配句柄。我不认为在您编辑控件之前它真的需要一个,并且它可能足够聪明来做到这一点,因为为不活动的控件分配太多资源会使其难以在一个控件上拥有大量控件表单(例如,想想 Excel)。

回答by gimel

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

Process Monitor在交互式监控 Windows 进程使用的各种资源方面非常方便。

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

Process Monitor 是 Windows 的高级监控工具,可显示实时文件系统、注册表和进程/线程活动。

Note - if you mean finding the information programatically, .Netprovides access to all performance counters. You use the System.Diagnostics.PerformanceCounterClass like this:

注意 - 如果您的意思是以编程方式查找信息,.Net提供对所有性能计数器的访问。您可以像这样使用System.Diagnostics.PerformanceCounter类:

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

回答by computinglife

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

taskmanager 显示的句柄数与 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.

来自 sysinternals 的 ProcessExplorer 工具可以列出进程使用的不同类型的句柄 + 它们的名称,您可以通过浏览有关程序使用的句柄组成的列表来获得一个好主意。

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.

要查看实际的句柄及其类型使用 ProcessExplorer - 查看 - 显示下部窗格视图 - 句柄。

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)

您还可以使用某种窗口间谍工具来显示系统中的所有窗口,例如 Microsoft spy++ 或 Managed Spy++ ( http://msdn.microsoft.com/en-us/magazine/cc163617.aspx)

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

这将允许您查看是否正在创建您的窗口。

回答by user24770

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.).

Perfmon 或任务管理器无法为您提供进程使用的 WINDOW 句柄数,而只能提供所有类型(文件、线程等)的句柄总数。

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

我能找到的关于该主题的最佳信息是这篇文章,它表明可以通过枚举主进程窗口的所有子窗口来确定进程的窗口句柄计数。