Files
ShrlAlgoToolkit/ShrlAlgo.Toolkit.Core/Heplers/SingletonViewHelper.cs

33 lines
754 B
C#

using System.Windows;
namespace ShrlAlgo.Toolkit.Core.Heplers;
public sealed record SingletonViewHelper<T>
where T : Window, new()
{
private static T _instance;
private static readonly object Padlock = new();
private SingletonViewHelper() { }
public static T GetInstance(out bool isNewCreate)
{
isNewCreate = false;
//double-check locking
if (_instance == null)
{
isNewCreate = true;
lock (Padlock)
{
_instance ??= new T();
_instance.Closed += OnWindowClosed;
}
}
return _instance;
}
private static void OnWindowClosed(object sender, EventArgs e)
{
_instance = null;
}
}