I have code for a thread-safe Singleton as follows:
[FONT="Courier New"]
public sealed class Singleton
{
private static volatile Singleton instance;
private static object syncRoot = new Object();
private Singleton() {}
public static Singleton Instance
{
get
{
if (instance == null)
{
lock (syncRoot)
{
if (instance == null)
instance = new Singleton();
}
}
return instance;
}
}
}
[/FONT]
[FONT="Courier New"] Singleton single = Singleton.Instance; if (single == null) // ... Exit appropriately [/FONT]
[FONT="Courier New"]
public static Mutex mutex = new Mutex(true,"MyDogHasFleas") ;
public static bool SingleOut()
{
if (mutex.WaitOne(TimeSpan.Zero,true))
{
mutex.ReleaseMutex() ;
return true ;
}
else
{
MessageBox.Show("Only one instance at a time!") ;
return false ;
}
}
[/FONT]
[FONT="Courier New"] bool solo = SingleOut(); if (!solo) // ... Exit appropriately [/FONT]
I cannot seem to make either approach work.In both approaches, I can run a strategy several times and each runs without detecting any previous instance.
I am obviously doing something wrong and would appreciate advice on getting both approaches working in NT7 and NT8.
Thanks.

Comment