c# - 异常 "Cannot access a disposed object"来自非用户代码

标签 c# .net vb.net winforms mdi

我通常理解异常无法访问已处置的对象。但这一次它似乎源自非用户代码。我没有发布我的代码,因为这次我不知道是哪一个:)关闭表单时偶尔会发生崩溃。看起来一些自动 UI 刷新正在进行中:

Message: Cannot access a disposed object.
Object name: 'Icon'.
HResult: -2146232798
HelpLink: (null)
Source: System.Drawing
TargetSite: IntPtr get_Handle()
StackTrace: 
       at System.Drawing.Icon.get_Handle()
       at System.Drawing.Icon.get_Size()
       at System.Drawing.Icon.ToBitmap()
       at System.Windows.Forms.MdiControlStrip.GetTargetWindowIcon()
       at System.Windows.Forms.MdiControlStrip..ctor(IWin32Window target)
       at System.Windows.Forms.Form.UpdateMdiControlStrip(Boolean maximized)
       at System.Windows.Forms.Form.UpdateToolStrip()
       at System.Windows.Forms.Form.OnMdiChildActivate(EventArgs e)
       at System.Windows.Forms.Form.ActivateMdiChildInternal(Form form)
       at System.Windows.Forms.Form.WmMdiActivate(Message& m)
       at System.Windows.Forms.Form.WndProc(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
       at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
       at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
       --end of stack trace--

有可能这个仅在调试器 session 中抛出,到目前为止我还没有在生产中看到它。 (请参阅堆栈跟踪的最底部项目。)

  • 我的假设(这仅是由调试 session 引起的)正确吗?或者我应该担心生产中的同样问题吗?

  • 这个问题可以通过某种方式避免吗?

最佳答案

您提供的调用堆栈可能与已知的 .Net 问题相关。请参阅following thread at MSDN-forum .
可能的解决方案是在子表单的 FormClosing 事件处理程序中使用 Me.Hide

关于c# - 异常 "Cannot access a disposed object"来自非用户代码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28699172/

相关文章:

.net - 如何从 .NET 锁定 Windows(如 "Windows + L")?

asp.net - jquery ajax发布问题在警报中获取空值

asp.net - VB.NET 接口(interface)

c# - EF Core "Cannot insert explicit value for identity column"尽管 IDENTIY_INSERT 设置为 ON

C# WPF MVVM 组合框绑定(bind)

c# - 如何从字符串中删除无关紧要的零?

c# - 在线程应用程序中使用 C# Volatile 关键字

c# - 如何修复 WebBrowser 控件上 DrawToBitmap 的不透明度错误?

c# - 在 C# 中,int 必须是正数、负数还是零?

C#,在List<>.ConvertAll方法中使用lambda表达式,当对象为null时如何进行过滤?