System.ObjectDisposeException:无法访问已处置的对象 - 为什么会发生?
我收到堆栈跟踪错误...
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)
产生此错误的代码是...
Friend Sub GoHome(ByVal sender As Form)
InTransit = True
sender.Close()
fMain.Show()
End Sub
当我只是切换 .show 和 .close 方法的顺序时,它不会给出错误
Friend Sub GoHome(ByVal sender As Form)
InTransit = True
fMain.Show()
sender.Close()
End Sub
您能告诉我为什么吗?在这种情况下它会给出错误,为什么在第二种情况下它不会?
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
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??
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在这种情况下,
sender
和fMain
是同一个对象吗?如果是这样...当您调用
sender.Close
时,您实际上是在调用fMain.Close
,并且Close
方法将处置该对象幕后。如果您随后调用fMain.Show
,那么您是在刚刚处理的对象上调用它,因此会出现错误。或者,或者...
也许
sender
是fMain
上的子控件之一?您调用
sender.Close
,处置子控件。然后,您调用fMain.Show
,它尝试对属于fMain
的子控件执行某些操作。当它尝试使用您刚刚处理的子控件执行特定操作时,就会发生错误。Are
sender
andfMain
the same object in this case?If so... when you call
sender.Close
you're effectively callingfMain.Close
, and theClose
method will be disposing that object behind-the-scenes. If you subsequently callfMain.Show
then you're calling it on an object that you just disposed, hence the error.Or, alternatively...
Maybe
sender
is one of the child controls onfMain
?You call
sender.Close
, disposing the child control. You then callfMain.Show
which attempts to do something with the child controls belonging tofMain
. The error occurs when it tries to do that particular something with the child control that you just disposed.