c# - C#/。NET ThreadException-创建窗口句柄时出错

标签 c# multithreading winforms error-handling

我最近在我的C#(WinForms)应用程序中实现了一些(基本)未处理的错误处理程序。 UnhandledExceptionEventHandler工作正常,但是在ThreadExceptionEventHandler中,我遇到了一些奇怪的事情。

在关闭应用程序时,通过this.Close(),标准WinForms关闭按钮,Application.Exit()等在关闭应用程序时会引起非常短暂的错误。

堆栈跟踪已记录,但是它给我的是:

System.ComponentModel.Win32Exception (0x80004005): Error creating window handle.
at System.Windows.Forms.NativeWindow.CreateHandle(CreateParams cp)
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.CreateGraphicsInternal()
at System.Windows.Forms.ThreadExceptionDialog..ctor(Exception t)
at System.Windows.Forms.Application.ThreadContext.OnThreadException(Exception t)
at System.Windows.Forms.Control.WndProcException(Exception e)
at System.Windows.Forms.Control.ControlNativeWindow.OnThreadException(Exception e)
at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

然后,该应用程序似乎看起来很正常地关闭。我最好的猜测是某种形式的内存泄漏或未正确处理的任务关闭,但是到目前为止,我在互联网上找不到的任何帮助都没有。

任何想法和建议表示赞赏。提前致谢

更新:有趣的是,该错误已停止发生...不知道为什么。但是,我预计它可能会返回。更长远的解决方案将是美好的。

最佳答案

如bansi所说:如果要在WinForm应用程序中关闭表单,请确保先前已关闭所有使用该表单的线程。堆栈跟踪的最后一行(System.Windows.Forms.NativeWindow.Callback)提供了提示,线程试图将消息发送到已关闭且不再具有窗口句柄的窗体。

关于c# - C#/。NET ThreadException-创建窗口句柄时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41195874/

相关文章:

c# - CaSTLe Windsor/DelegatingHandler/IPrincipal 依赖注入(inject) (DI)/ASP.NET Web API 中的控制反转 (IoC)

c# - 在 C# 中加速将复杂类型转换为另一种复杂类型以进行序列化

java - Jmeter行为怪癖

c# - 如何获取我的 Windows 窗体应用程序已本地化为的所有语言的列表?

c# - 从单独的表单调用 MessageBox.Show

c# - 列表中的元素不会写入文本框

c# - 重构异步/等待并行处理

c# - 在 C# 中更改 "Override high DPI scaling behavior"

python - 并行化Python列表理解并没有提高性能

multithreading - TThread.OnTerminate执行线程