I am trying to create a singleton which has a non-empty constructor and to access it into a synchronized way: since I have no control on the order of initialization of my component, if a component access the singleton before it's initialized, it has to wait.
The factory method is called just once, and my initilization phase throws an exception
private static volatile GottwareExcelAddin _instance;
private static readonly ManualResetEvent InitializedEvent=new ManualResetEvent(false);
internal static Singleton CurrentInstance
{
get
{
InitializedEvent.WaitOne();
return _instance;
}
}
#endregion
private Singleton(String url, Int otherstuff)
{
// do stuff
InitializedEvent.Set();
}
#region public factory
[OnWorkerThread]
public static void Singleton(String spaceUrl, _Application excelApp)
{
if (_instance == null)
_instance = new Singleton(spaceUrl, excelApp);
}
The [OnWorkerThread] is an attribute that makes the factory run on a worker thread, and the factory is called only once.
When I launch myapp, sometimes I get the following:
Exception Source: mscorlib Exception Type: System.Runtime.InteropServices.SEHException Exception Message: External component has thrown an exception. Exception Target Site: WaitOneNative
---- Stack Trace ---- System.Threading.WaitHandle.WaitOneNative(waitableSafeHandle As SafeHandle, millisecondsTimeout As UInt32, hasThreadAffinity As Boolean, exitContext As Boolean) AddinExpress.RTD.2005.dll: N 00000 (0x0) JIT System.Threading.WaitHandle.InternalWaitOne(waitableSafeHandle As SafeHandle, millisecondsTimeout As Int64, hasThreadAffinity As Boolean, exitContext As Boolean) AddinExpress.RTD.2005.dll: N 0020 (0x14) IL System.Threading.WaitHandle.WaitOne(millisecondsTimeout As Int32, exitContext As Boolean)
What am I do wrong?