你如何获取当前窗口句柄计数和窗口句柄限制在.NET? [英] How do you obtain Current Window Handle Count and Window Handle Limit in .NET?

查看:173
本文介绍了你如何获取当前窗口句柄计数和窗口句柄限制在.NET?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得窗口句柄的当前数量,并在C#全系统窗口句柄限制。我怎么去呢?

I want to obtain the current number of window handles and the system-wide window handle limit in C#. How do I go about this?

推荐答案

如果你读Raymond Chen的岗位,你可能会发现像我这样做是因为讨厌。你只是可能做得不对,因为你所做的是Windows是没有能力的。

If you read Raymond Chen's post, you'll probably find it as annoying as I did. You're only "probably doing something wrong" because you're doing something Windows isn't capable of.

在我的应用程序,在用户第一次访问一个标签页,创建和布局该网页上的所有控件。这需要时间明显量 - 也可以很容易地在页面上50控制。因此,我不放弃一个标签页上的控件填充后,如果它是在所有可能的,并留下关闭套标签页到用户。

In my application, the first time a user visits a tab page, I create and lay out all the controls on that page. This takes a noticeable amount of time - there can easily be 50 controls on a page. So I don't discard the controls on a tab page after populating it, if it's at all possible, and leave closing sets of tab pages up to the user.

碰巧的是,有些用户从来没有要关闭的任意的套标签页。为什么我要强迫他们?随着我的用户界面,就可以非常快速导航到的300多套的交易,他们是负责管理的任何一个。他们的机器足够快,并且有足够的内存,使这一切都非常敏感。唯一的问题是,Windows不支持它。

As it happens, some users never want to close any sets of tab pages. Why should I be forcing them to? With my UI, they can navigate very quickly to any one of the 300+ sets of transactions that they're responsible for managing. Their machines are fast enough, and have enough memory, to make this all very responsive. The only problem is that Windows can't support it.

为什么我用我的控制,而不是其他一些UI技术?因为他们的工作的。我需要支持的重点事件,tab顺序,验证事件,动态布局,和数据绑定 - 将用户的实际管理记录成千上万,在几十个表,在内存中的数据集。发展我不得不做的量 - 说 - 实现一些使用窗口控件是一个天文数字。

Why am I using controls, and not some other UI technology? Because they work. I need to support focus events, tab order, validation events, dynamic layout, and data binding - the users are actually managing thousands of records, in dozens of tables, in an in-memory DataSet. The amount of development I'd have to do to - say - implement something using windowless controls is astronomical.

我只是做错了,因为Windows对窗口数量的硬性限制处理,它可以支持。这个硬限制是根据一串有关如何将计算机的用户界面可能会被内置十年之久的假设。这不是我的谁是做错了什么。

I'm only "doing it wrong" because Windows has a hard limit on the number of window handles that it can support. That hard limit is based on a bunch of decade-old assumptions about how a computer's UI might be built. It's not me who's "doing something wrong."

目前无论如何,我的解决办法是在两部分。

At any rate, my solution to this is in two parts.

首先,一类,它可以告诉你有多少个窗口句柄的进程正在使用:

First, a class that can tell you how many window handles your process is using:

using System;
using System.Runtime.InteropServices;

namespace StreamWrite.Proceedings.Client
{
    public class HWndCounter
    {
        [DllImport("kernel32.dll")]
        private static extern IntPtr GetCurrentProcess();

        [DllImport("user32.dll")]
        private static extern uint GetGuiResources(IntPtr hProcess, uint uiFlags);

        private enum ResourceType
        {
            Gdi = 0,
            User = 1
        }

        public static int GetWindowHandlesForCurrentProcess(IntPtr hWnd)
        {
            IntPtr processHandle = GetCurrentProcess();
            uint gdiObjects = GetGuiResources(processHandle, (uint)ResourceType.Gdi);
            uint userObjects = GetGuiResources(processHandle, (uint)ResourceType.User);

            return Convert.ToInt32(gdiObjects + userObjects);
        }
    }
}

二,我坚持我的标签页对象的最近最少使用的高速缓存。在.NET框架没有提供一个通用的LRU缓存类,所以我建的,你可以在这里得到 如果你需要一个。用户访问一个标签页每一次,我将它添加到LRU缓存。然后我检查,看看是否我的窗口句柄所剩无几。如果我是,我扔掉了控制最近最少使用的标签页,并保持这样做,直到我有足够的窗口中再次处理。

Second, I maintain a least-recently-used cache of my tab page objects. The .NET framework doesn't provide a generic LRU cache class, so I built one, which you can get here if you need one. Every time the user visits a tab page, I add it to the LRU Cache. Then I check to see if I'm running low on window handles. If I am, I throw away the controls on the least-recently-used tab page, and keep doing that until I have enough window handles again.

这篇关于你如何获取当前窗口句柄计数和窗口句柄限制在.NET?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆