System.ObjectDisposeException:无法访问已处置的对象 - 为什么会发生?

发布于 2025-01-06 10:10:48 字数 1223 浏览 1 评论 0原文

我收到堆栈跟踪错误...

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情归归情 2025-01-13 10:10:48

在这种情况下,senderfMain 是同一个对象吗?

如果是这样...当您调用 sender.Close 时,您实际上是在调用 fMain.Close,并且 Close 方法将处置该对象幕后。如果您随后调用 fMain.Show ,那么您是在刚刚处理的对象上调用它,因此会出现错误。

或者,或者...

也许senderfMain上的子控件之一?

您调用sender.Close,处置子控件。然后,您调用fMain.Show,它尝试对属于fMain 的子控件执行某些操作。当它尝试使用您刚刚处理的子控件执行特定操作时,就会发生错误。

Are sender and fMain the same object in this case?

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. If you subsequently call fMain.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 on fMain?

You call sender.Close, disposing the child control. You then call fMain.Show which attempts to do something with the child controls belonging to fMain. The error occurs when it tries to do that particular something with the child control that you just disposed.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文