我有一个通常可以正常运行的 Windows 窗体应用程序,但每隔一段时间(<1% 的时间)我就会看到这样的错误:
与视觉样式相关的操作导致错误,因为当前没有任何视觉样式处于事件状态。在
System.Windows.Forms.VisualStyles.VisualStyleRenderer.get_Handle()
在 System.Windows.Forms.VisualStyles.VisualStyleRenderer.DrawBackground(IDeviceContext dc,矩形边界,矩形 clipRectangle)
在 System.Windows.Forms.GroupBoxRenderer.DrawThemedGroupBoxWithText(图形 g、矩形边界、字符串 groupBoxText、字体字体、颜色文本颜色、TextFormatFlags 标志、GroupBoxState 状态)
在 System.Windows.Forms.GroupBoxRenderer.DrawGroupBox(图形 g、矩形边界、字符串 groupBoxText、字体字体、TextFormatFlags 标志、GroupBoxState 状态)
在 System.Windows.Forms.GroupBox.OnPaint(PaintEventArgs e)
在 System.Windows.Forms.Control.PaintWithErrorHandling(PaintEventArgs e,Int16 层)
在 System.Windows.Forms.Control.WmPaint(Message& m)
在 System.Windows.Forms.Control.WndProc(Message& m)
在 System.Windows.Forms.GroupBox.WndProc(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
在 System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
在 System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd,Int32 msg,IntPtr wparam,IntPtr lparam)
我之前处理过视觉样式错误,因此确保使用该应用程序的机器具有兼容的桌面主题。此错误的奇怪之处在于,在抛出此异常之前,程序会启动并愉快地运行一段时间(例如 30 分钟)。我不明白视觉样式的状态在执行期间如何改变。
该应用程序由启动 Windows 窗体的控制台应用程序组成,该窗体使用 Awesomium加载网页并响应 javascript 回调。
我正在使用 ThreadExceptionEventHandler
监听控制台应用程序上的 Application.ThreadException
来捕获错误。我突然想到,当错误发生时,Windows 窗体可能已经被处理掉了,但我不认为是这种情况,因为当我记录错误时,我可以访问窗体的实例成员,例如。 myForm.ToString()
由于堆栈跟踪中没有我的代码,所以我不确定还有什么可以帮助描述此错误。有没有人见过这样的事情?
最佳答案
Hans Passant 是正确的,这是 GDI 句柄泄漏。我无法在我的开发环境中重现它,因此需要向我的服务器添加一些日志记录。万一它对其他人有帮助,这里是我如何通过调用 GetGuiResources 获得我需要的信息功能。
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetGuiResources(IntPtr hProcess, int uiFlags);
并使用它来获取句柄数:
var p = Process.GetCurrentProcess();
kernel = p.HandleCount;
gdiObjects = GetGuiResources(p.Handle, 0);
userObjects = GetGuiResources(p.Handle, 1);
gdiObjectsPeak = GetGuiResources(p.Handle, 2);
userObjectsPeak = GetGuiResources(p.Handle, 4);
就位后,我看到 GDI 对象在崩溃发生时处于 10,000 上限。
关于c# - Windows 窗体在执行中抛出与视觉样式相关的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17224115/