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

时间:2016-12-17 06:10:08

标签: 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)

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

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

更新:有趣的是,错误已经停止发生......不确定原因。但是,我预计它可能会回归。一个更长期的解决方案将是美好的。

1 个答案:

答案 0 :(得分:0)

正如bansi所说:如果要关闭WinForm应用程序中的表单,请确保使用此表单的所有线程都先关闭。堆栈跟踪的最后一行(System.Windows.Forms.NativeWindow.Callback)给出提示,一个线程尝试向已经关闭且没有窗口句柄的表单发送消息。