简体   繁体   English

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

[英]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? 在这种情况下, senderfMain是否是同一对象?

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 ? 也许senderfMain上的子控件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.

相关问题 System.ObjectDisposedException:无法访问已处置的对象。 AplicationDbContext - System.ObjectDisposedException: Cannot access a disposed object. AplicationDbContext System.ObjectDisposedException:'无法访问已处置的对象。' - System.ObjectDisposedException: 'Cannot access a disposed object.' .NET 3.5 C#Bug with System.Timer System.ObjectDisposedException:无法访问已处置的对象 - .NET 3.5 C# Bug with System.Timer System.ObjectDisposedException: Cannot access a disposed object DbContext Depedency Injection Issue - System.ObjectDisposedException:无法访问已处置的对象 - DbContext Depedency Injection Issue - System.ObjectDisposedException: Cannot access a disposed object 无法在 .NET MaUI 中调整 IImage 的大小,“System.ObjectDisposedException:无法访问已处置的 object。Object 名称:Android.Graphics.Bitmap” - Not able to Resize an IImage in .NET MaUI, "System.ObjectDisposedException: Cannot access a disposed object. Object name: Android.Graphics.Bitmap" 类型'System.ObjectDisposedException'的异常 - Exception of type 'System.ObjectDisposedException' 实体框架System.ObjectDisposedException - Entity Framework System.ObjectDisposedException 如何压缩/捕获System.ObjectDisposedException? - How to suppress/catch System.ObjectDisposedException? 简单表单显示中的System.ObjectDisposedException - System.ObjectDisposedException from simple form display 无法访问已处置的对象 - Cannot access a disposed object
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM