Pergunta

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?

Foi útil?

Solução

You haven't created a proper singleton there. A proper singleton should have a static method to access it, and a private constructor.

Another issue is that if you have parameters with which to initialise a singleton, what does it mean if you initialise the singleton more than once with different parameters? Is it an error?

Assuming that you can just ignore multiple initialisations with different parameters, you could implement it as follows (but I think you may have a design error somewhere):

class Singleton
{
    public static Singleton Instance(string param1, int param2)
    {
        if (_instance == null)
        {
            lock (_locker)
            {
                if (_instance == null) // Double-checked locking (works in C#!).
                {
                    _instance = new Singleton(param1, param2);
                }
            }
        }

        return _instance;
    }

    private Singleton(string param1, int param2)
    {
        // Whatever.
    }

    private static Singleton _instance;
    private static readonly object _locker = new object();
}

I normally use Lazy to implement singletons, but it isn't so helpful if your singleton's constructor requires parameters passed in at the point of access of your singleton.

Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top