我有一个这样的表单,我想使用单例来确保只有一个实例正在运行
private static productForm instance;
public productForm()
{
InitializeComponent();
}
public static productForm GetInstance
{
get
{
if (instance == null)
{
instance = new productForm();
}
return instance;
}
}
/*private void productForm_FormClosed (object sender, FormClosingEventArgs e)
{
instance = null;
}*/
}
在主要表单点击事件
private void categoryTreeToolStripMenuItem_Click(object sender, EventArgs e)
{
productForm.GetInstance.Show();
}
当我关闭productform时有一个错误,当我尝试重新打开它时出错。
productForm.GetInstance.Show();将输出错误消息“无法访问已处置的对象”,似乎productForm无法访问,任何人都知道是什么问题?
答案 0 :(得分:4)
试试这个
private static productForm instance;
public productForm()
{
InitializeComponent();
}
public static productForm GetInstance
{
get
{
if (instance == null || instance.IsDisposed)
{
instance = new productForm();
}
return instance;
}
}