无法访问实例 VB.net 中的已处理对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16355612/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Cannot access a disposed object in instance VB.net
提问by Vinra Gunanta Pandia
i have problem with my project, i have 2 form, 1 form with MDI container = trueand 1 form is child form.
i have code in parent form like below
我的项目有问题,我有 2 个表单,1 个表单MDI container = true和 1 个表单是子表单。我有如下父表单的代码
Public Class frmInduk
Dim afrmDaftarBarang As frmDaftarBarang = frmDaftarBarang.instance
Private Sub DataBarangToolStripMenuItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles DataBarangToolStripMenuItem.Click
With afrmDaftarBarang
.MdiParent = Me
'Dim afrmDaftarBarang As frmDaftarBarang = frmDaftarBarang.instance
.Show()
.Focus()
End With
End Sub
End Class
and than, in my child form i have code like below
然后,在我的孩子形式中,我有如下代码
Public Class frmDaftarBarang
Private Shared anInstance As frmDaftarBarang
Public Shared ReadOnly Property instance() As frmDaftarBarang
Get
If anInstance Is Nothing Then
anInstance = New frmDaftarBarang
End If
Return anInstance
End Get
End Property
End Class
when i run this project, it no problem . my project can run very well. but the truble come when i call frmDataBarang, and then i close, but when i call frmDataBarangagain
i get some error lice picture bellow
当我运行这个项目时,没问题。我的项目可以运行得很好。但是当我打电话时麻烦来了frmDataBarang,然后我关闭,但是当我frmDataBarang再次打电话时,我得到了一些错误的虱子图片


i hope anybody can help me :(
我希望任何人都可以帮助我:(
回答by Hans Passant
You are trying to use the Singleton pattern, but you did it imperfectly. An MDI child form can be created more than once, you will have to set the singleton instance back to Nothing when it gets destroyed. Do so with the FormClosing event:
您正在尝试使用单例模式,但您做得并不完美。可以多次创建 MDI 子窗体,当它被销毁时,您必须将单例实例设置回 Nothing。使用 FormClosing 事件执行此操作:
Protected Overrides Sub OnFormClosed(ByVal e As FormClosedEventArgs)
anInstance = Nothing
MyBase.OnFormClosed(e)
End Sub
You will also need to do something reasonable when an existing instance is displayed again. It might not be in the right state. This code reallybelongs in the MDI parent, but you can limp along with this:
当再次显示现有实例时,您还需要做一些合理的事情。它可能处于不正确的状态。这段代码确实属于 MDI 父级,但您可以同时跛行:
Get
If anInstance Is Nothing Then
anInstance = New frmDaftarBarang
Else
If anInstance.WindowState = FormWindowState.Minimized Then
anInstance.WindowState = FormWindowState.Normal
End If
End If
Return anInstance
Do note that this gets ugly when you use the singleton to do things other than child activation. It is really rather a bad idea to use the pattern. It is fine on your MdiParent, there can only ever be one parent and when it gets closed then the program terminates anyway. But those same rules don't apply to a child. Move child instance management code to the parent class, that's where it belongs.
请注意,当您使用单例执行子激活以外的操作时,这会变得很丑陋。使用这种模式确实是一个坏主意。在你的 MdiParent 上没问题,只能有一个父级,当它关闭时,程序无论如何都会终止。但同样的规则不适用于孩子。将子实例管理代码移到父类,这就是它所属的地方。
回答by Frosty840
When your class is called a second time, the instance is not Nothing, because it still exists, in a disposed state. You must add a check to see whether it is disposed.
当您的类第二次被调用时,该实例不是Nothing,因为它仍然存在,处于已处理状态。您必须添加检查以查看它是否已处理。
This may work:
这可能有效:
Public Class frmDaftarBarang
Private Shared anInstance As frmDaftarBarang
Public Shared ReadOnly Property instance() As frmDaftarBarang
Get
If anInstance Is Nothing OrElse anInstance.IsDisposed Then
anInstance = New frmDaftarBarang
End If
Return anInstance
End Get
End Property
End Class
回答by Paul
Instead of trying to show the disposed form just create a new one each time.
不要试图显示已处理的表单,只需每次创建一个新表单即可。
Dim newForm as New frmDaftarBarang
newForm.Show()
回答by Stefan ?or?evi?
This may happen if form you're calling causes exception.
如果您正在调用的表单导致异常,则可能会发生这种情况。
In this case fix any errors in form load and show events in form you are calling.
在这种情况下,修复表单加载中的任何错误并以您正在调用的表单显示事件。

