[英]I have this problem with a singleton form name FCD Cannot access a disposed object. Object name: 'FCD'.'
code error here this is an other form who content a button to lunch the FCD form这里的代码错误这是另一种形式,其中包含一个按钮以午餐 FCD 形式
private void Button1_Click(object sender, EventArgs e)
{
f = FCD.getInstance();
f.Show();
}
and the class FDC is class FDC 是
public partial class FCD : Form
{
private static FCD instance=null;
private FCD()
{
InitializeComponent();
load();
}
public static FCD getInstance()
{
if(instance == null)
{
instance = new FCD();
}
return instance;
}
the problem is when i execute the form fcd it work for the first time when i close it i try again and they give me this problem问题是当我执行 fcd 表单时,它第一次工作,当我关闭它时我再试一次,他们给了我这个问题
Check if instance IsDisposed
.检查实例
IsDisposed
是否。 Try the following:尝试以下操作:
Change from :从更改:
public static FCD getInstance()
{
if(instance == null)
{
instance = new FCD();
}
return instance;
}
To :至:
public static FCD getInstance()
{
if(instance == null || instance.IsDisposed)
{
instance = new FCD();
}
return instance;
}
so after a big discussion i found this solution所以经过一番大讨论后,我找到了这个解决方案
private void Button1_Click(object sender, EventArgs e)
{
if(f!=null)
if (f.IsDisposed)
{
FCD.setInstanceNull();
}
f = FCD.getInstance();
f.Show();
}
and i add a methode who set instance = null if form closed如果表单关闭,我添加一个设置实例 = null 的方法
public static void setInstanceNull()
{
instance = null;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.