C# Winforms 问题 - 创建窗口句柄时出错
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/222649/
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
Winforms issue - Error creating window handle
提问by leora
We are seeing this error in a Winform application. Can anyone help on why you would see this error, and more importantly how to fix it or avoid it from happening.
我们在 Winform 应用程序中看到此错误。任何人都可以帮助解释为什么您会看到此错误,更重要的是如何修复它或避免它发生。
System.ComponentModel.Win32Exception: Error creating window handle. at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp) at System.Windows.Forms.Control.CreateHandle() at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible) at System.Windows.Forms.Control.CreateControl() at System.Windows.Forms.Control.OnVisibleChanged(EventArgs e) at System.Windows.Forms.ButtonBase.OnVisibleChanged(EventArgs e)
采纳答案by itsmatt
Have you run Process Explorer or the Windows Task Manager to look at the GDI Objects, Handles, Threads and USER objects? If not, select those columns to be viewed (Task Manager choose View->Select Columns... Then run your app and take a look at those columns for that app and see if one of those is growing really large.
您是否运行过进程资源管理器或 Windows 任务管理器来查看 GDI 对象、句柄、线程和用户对象?如果没有,请选择要查看的那些列(任务管理器选择“查看”->“选择列”...然后运行您的应用程序并查看该应用程序的那些列,看看其中一个列是否变得非常大。
It might be that you've got UI components that you thinkare cleaned up but haven't been Disposed.
可能是您有一些您认为已清理但尚未处理的UI 组件。
Here's a linkabout this that might be helpful.
Good Luck!
祝你好运!
回答by AtliB
I think it's normally related to the computer running out of memory so it's not able to create any more window handles. Normally windows starts to show some strange behavior at this point as well.
我认为这通常与计算机内存不足有关,因此无法创建更多窗口句柄。通常 Windows 在这一点上也开始表现出一些奇怪的行为。
回答by rice
The out of memory suggestion doesn't seem like a bad lead.
内存不足的建议似乎不是一个糟糕的线索。
What is your program doing that it gets this error?
你的程序在做什么导致这个错误?
Is it creating a great many windows or controls? Does it create them programatically as opposed to at design time? If so, do you do this in a loop? Is that loop infinite? Are you consuming staggering boatloads of memory in some other way?
它是否创建了大量窗口或控件?它是否以编程方式而不是在设计时创建它们?如果是这样,您是否循环执行此操作?那个循环是无限的吗?您是否以其他方式消耗了惊人的大量内存?
What happens when you watch the memory used by your application in task manager? Does it skyrocket to the moon? Or better yet, as suggested above use process monitor to dive into the details.
当您在任务管理器中查看应用程序使用的内存时会发生什么?它会飞上月球吗?或者更好的是,如上所述,使用进程监视器来深入了解细节。
回答by mjezzi
The windows handle limit for your application is 10,000 handles. You're getting the error because your program is creating too many handles. You'll need to find the memory leak. As other users have suggested, use a Memory Profiler. I use the .Net Memory Profiler as well. Also, make sure you're calling the dispose method on controls if you're removing them from a form beforethe form closes (otherwise the controls won't dispose). You'll also have to make sure that there are no events registered with the control. I myself have the same issue, and despite what I already know, I still have some memory leaks that continue to elude me..
您的应用程序的 Windows 句柄限制为 10,000 个句柄。您收到错误是因为您的程序创建了太多句柄。您需要找到内存泄漏。正如其他用户所建议的那样,使用内存分析器。我也使用 .Net Memory Profiler。此外,如果在窗体关闭之前从窗体中删除控件,请确保调用控件的 dispose 方法(否则控件不会处理)。您还必须确保没有向控件注册的事件。我自己也有同样的问题,尽管我已经知道,我仍然有一些内存泄漏继续躲避我..
回答by Fabrice
See this post of mine about "Error creating window handle"and how it relates to USER Objects and the Desktop Heap. I provide some solutions.
请参阅我的这篇关于“错误创建窗口句柄”以及它与用户对象和桌面堆的关系的帖子。我提供一些解决方案。
回答by Fabrice
I got same error in my application.I am loading many controls in single page.In button click event i am clearing the controls.clearing the controls doesnot release the controls from memory.So dispose the controls from memory. I just commented controls.clear() method and include few lines of code to dispose the controls. Something like this
我在我的应用程序中遇到了同样的错误。我在单个页面中加载了许多控件。在按钮单击事件中,我正在清除控件。清除控件不会从内存中释放控件。因此从内存中处理控件。我刚刚评论了 controls.clear() 方法并包含了几行代码来处理控件。像这样的东西
for each ctl as control in controlcollection
对于每个 ctl 作为 controlcollection 中的控件
ctl.dispose()
ctl.dispose()
Next
下一个
回答by AlfredBr
This problem is almost always related to the GDI Object count, User Object count or Handle count and usually notbecause of an out-of-memory condition on your machine.
这个问题几乎总是与 GDI 对象计数、用户对象计数或句柄计数有关,通常不是因为机器上的内存不足情况。
When I am tracking one of these bugs, I open ProcessExplorer and watch these columns: Handles, Threads, GDI Objects, USER Objects, Private Bytes, Virtual Size and Working Set.
当我跟踪这些错误之一时,我打开 ProcessExplorer 并查看这些列:句柄、线程、GDI 对象、用户对象、私有字节、虚拟大小和工作集。
(In my experience, the problem is usually an object leak due to an event handler holding the object and preventing it from being disposed.)
(根据我的经验,问题通常是由于事件处理程序持有对象并阻止其被处理而导致的对象泄漏。)
回答by kingsley
Well, in my case it was definitely the USER Objects that were out of control. I looked in the Windows Task Manager and sure enough, the USER Objects count was at 10'000 exactly.
好吧,就我而言,肯定是失控的 USER 对象。我查看了 Windows 任务管理器,果然,用户对象计数正好是 10'000。
I am dynamically embedding property and list sheets in Tab Pages by setting the Parent property of the property or list sheet's container panel to that of the Tab Page. I am conditionally recycling or re-creating the property and list sheet forms depending on the type of collection being listed or class type of the object being inspected.
我通过将属性或列表工作表的容器面板的父属性设置为标签页的父属性,在标签页中动态嵌入属性和列表工作表。我正在有条件地回收或重新创建属性和列表表单,具体取决于列出的集合类型或被检查对象的类类型。
NB: In Delphi, all controls had an Owner and a Parent property. Even if one changed the Parent property of a control, it would still be disposed by its owner when the owning control got destroyed.
注意:在 Delphi 中,所有控件都有一个 Owner 和一个 Parent 属性。即使更改了控件的 Parent 属性,当拥有的控件被销毁时,它仍然会被其所有者处置。
In C# it seems that if a control e.g. a Panel is programmatically reassigned from, say, a Form to a Tab Page by changing the Panel.Parent property, calling Dispose() on the Form won't dispose the Panel, neither will calling Controls.Clear() on the Tab Page. Even a direct call Panel.Dispose() won't actually dispose it, unless its Parent is manually set to null beforehand.
在 C# 中,如果通过更改 Panel.Parent 属性以编程方式将控件(例如面板)从表单重新分配到选项卡页,则在表单上调用 Dispose() 不会处理面板,调用控件也不会.Clear() 在标签页上。即使直接调用 Panel.Dispose() 也不会实际处理它,除非事先将其 Parent 手动设置为 null。
回答by user344760
Definitely too many handles(memory leak issue):
肯定有太多的句柄(内存泄漏问题):
IT Jungles: System.ComponentModel.Win32Exception: Error creating window handle
回答by xlthim
I added a check that makes it work...
我添加了一个检查,使其工作...
if (_form.Handle.ToInt32() > 0)
{
_form.Invoke(method, args);
}
it is always true, but the form throws an error without it. BTW, my handle is around 4.9 million
它总是正确的,但是没有它表单会抛出错误。顺便说一句,我的手柄大约是 490 万