无法访问已释放的对象 - 如何解决? [英] Cannot access a disposed object - How to fix?

查看:4448
本文介绍了无法访问已释放的对象 - 如何解决?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

关闭表单时,在VB.NET的WinForms项目,我得到一个异常无法访问已释放的对象。它很少发生,我不能重新创建它的需求。堆栈跟踪看起来是这样的:

无法访问已释放的对象。对象名称:dbiSchedule。在System.Windows.Forms.Control.CreateHandle()在System.Windows.Forms.Control.get_Handle()在System.Windows.Forms.Control.PointToScreen(点P)在Dbi.WinControl.Schedule.dbiSchedule.a(布尔A_0)在Dbi.WinControl.Schedule.dbiSchedule.a(对象A_0,EventArgs的A_1)在System.Windows.Forms.Timer.OnTick(EventArgs e)上System.Windows.Forms.Timer.TimerNativeWindow.WndProc(消息和M)在System.Windows.Forms.NativeWindow.Callback(IntPtr的的HWND,味精的Int32,IntPtr的WPARAM,IntPtr的LPARAM)

该dbiSchedule距离DBI技术含量的计划控制。这里是更新时间表屏幕每隔几分钟就窗体上的计时器。
任何想法是什么原因造成的异常,我怎么可能去修复它?甚至只能够重新创建它的需求呢?

In a VB.NET WinForms project I get an exception "Cannot access a disposed object" when closing a form. It occurs very rarely and I cannot recreate it on demand. The stack trace looks like this:

Cannot access a disposed object. Object name: 'dbiSchedule'. at System.Windows.Forms.Control.CreateHandle() at System.Windows.Forms.Control.get_Handle() at System.Windows.Forms.Control.PointToScreen(Point p) at Dbi.WinControl.Schedule.dbiSchedule.a(Boolean A_0) at Dbi.WinControl.Schedule.dbiSchedule.a(Object A_0, EventArgs A_1) at System.Windows.Forms.Timer.OnTick(EventArgs e) at System.Windows.Forms.Timer.TimerNativeWindow.WndProc(Message& m) at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

The dbiSchedule is a schedule control from Dbi-tech. There is a timer on the form that updates the schedule on the screen every few minutes.
Any ideas what is causing the exception and how I might go about fixing it? or even just being able to recreate it on demand?

HEJ!感谢所有的答案。我们停止计时器上的FormClosing事件,我们确实在计时器Tick事件在使用前检查调度部件上的IsDisposed属性,但它并不能帮助。

这是一个非常恼人的问题,因为如果有人做拿出奏效的解决方案 - 我将无法确认解决方案,因为我无法手动重现问题

Hej! Thanks for all the answers. We do stop the Timer on the FormClosing event and we do check the IsDisposed property on the schedule component before using it in the Timer Tick event but it doesn't help.

It's a really annoying problem because if someone did come up with a solution that worked - I wouldn't be able to confirm the solution because I cannot recreate the problem manually.

推荐答案

尝试检查<一href="http://msdn.microsoft.com/en-us/library/system.windows.forms.control.isdisposed.aspx">IsDisposed访问控制之前财产。你也可以检查它的<一个href="http://msdn.microsoft.com/en-us/library/system.windows.forms.form.formclosing.aspx">FormClosing事件,假设你使用的FormClosed事件。

Try checking the IsDisposed property before accessing the control. You can also check it on the FormClosing event, assuming you're using the FormClosed event.

我们做站上的计时器   的FormClosing事件,我们做检查   上了日程IsDisposed财产   在定时器使用之前组件   勾选事件,但它并不能帮助。

We do stop the Timer on the FormClosing event and we do check the IsDisposed property on the schedule component before using it in the Timer Tick event but it doesn't help.

检查IsDisposed可能会有帮助,但要小心此之前调用GC.Collect的。阅读这篇文章,通过波多黎各马里亚尼何时调用GC.Collect()

Calling GC.Collect before checking IsDisposed may help, but be careful with this. Read this article by Rico Mariani "When to call GC.Collect()".

这篇关于无法访问已释放的对象 - 如何解决?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆