[英]System.ObjectDisposedException: Cannot access a disposed object - Why happening?
I got the error having the stack trace... 我有堆栈跟踪的错误...
System.ObjectDisposedException: Cannot access a disposed object.
Object name: 'Button'.
at System.Windows.Forms.Control.CreateHandle()
at System.Windows.Forms.Control.get_Handle()
at System.Windows.Forms.Control.PointToScreen(Point p)
at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ButtonBase.WndProc(Message& m)
at System.Windows.Forms.Button.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.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
The code producing this error is.... 产生此错误的代码是...。
Friend Sub GoHome(ByVal sender As Form)
InTransit = True
sender.Close()
fMain.Show()
End Sub
It doesn't give error when I just switch the order of the .show and .close method 当我只切换.show和.close方法的顺序时,它不会给出错误
Friend Sub GoHome(ByVal sender As Form)
InTransit = True
fMain.Show()
sender.Close()
End Sub
Can you please tell me why for the first case it gives error and why in second case it doesn't?? 您能告诉我为什么在第一种情况下会报错,为什么在第二种情况下却报错吗?
Are sender
and fMain
the same object in this case? 在这种情况下, sender
和fMain
是否是同一对象?
If so... when you call sender.Close
you're effectively calling fMain.Close
, and the Close
method will be disposing that object behind-the-scenes. 如果是这样的话...当您调用sender.Close
时,您sender.Close
是在调用fMain.Close
,并且Close
方法将在后台处理该对象。 If you subsequently call fMain.Show
then you're calling it on an object that you just disposed, hence the error. 如果随后调用fMain.Show
则将在刚刚处置的对象上调用它,因此会出现错误。
Or, alternatively... 或者,或者...
Maybe sender
is one of the child controls on fMain
? 也许sender
是fMain
上的子控件fMain
?
You call sender.Close
, disposing the child control. 您调用sender.Close
,处置子控件。 You then call fMain.Show
which attempts to do something with the child controls belonging to fMain
. 然后调用fMain.Show
它试图做属于子控件的东西 fMain
。 The error occurs when it tries to do that particular something with the child control that you just disposed. 当它尝试使用刚刚处置的子控件执行特定操作时,就会发生该错误。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.