c#中如何使用单例模式防止窗体重复打开
展开全部
public class SingletonWindow
{
private volatile static Singleton _instance = null;
private static readonly object lockHelper = new object();
private SingletonWindow(){}
public static SingletonWindow CreateInstance()
{
if(_instance == null)
{
lock(lockHelper)
{
if(_instance == null)
_instance = new SingletonWindow();
}
}
return _instance;
}
}
当你需要这个window 实例的时候使用SingletonWindow.CreateInstance() 就可以了
{
private volatile static Singleton _instance = null;
private static readonly object lockHelper = new object();
private SingletonWindow(){}
public static SingletonWindow CreateInstance()
{
if(_instance == null)
{
lock(lockHelper)
{
if(_instance == null)
_instance = new SingletonWindow();
}
}
return _instance;
}
}
当你需要这个window 实例的时候使用SingletonWindow.CreateInstance() 就可以了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询