Initialization of Multi-threaded singleton with parameters crashes

Answered Initialization of Multi-threaded singleton with parameters crashes

  • 2012年4月4日 7:48
     
     

    Dear forum,

    I am developing a set of add-ins for Excel. Since I have no guarantee of the order, I need to wait until a certain singleton becomes available before I could move further. I am trying to create a singleton which has a non-empty constructor and lock the components of my application which try to access it before it is ready through a ManualResetEvent, that leads, however, to a 

    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 

    I was unable to copy-paste here the code, but you could have a look to http://stackoverflow.com/questions/9954344/lazy-initialization-of-a-singleton-with-parameters

    Edmondo Porcu


すべての返信

  • 2012年4月4日 11:40
     
     回答済み コードあり

    Hmm, imho this is sufficient as the .Net  framework itself guarantees the correct instantiation:

    sealed class MySingleton
    {
        private static readonly MySingleton instance = new MySingleton();
    
        private MySingleton()
        {
            // Initialization goes here..
        }
    
        public static MySingleton Instance()
        {
            return instance;
        }
    }

    Also your code looks pretty incomplete. I'm not sure whether the exception comes from your quite weird looking event waiting strategy..

    p.s. the press the button with the <> marks and lines icon in the toolbar over the edit window.


  • 2012年4月4日 16:40
     
     回答済み

    Hi, 

    It seems there is an exception at unmanaged code, which is propogated to managed code  and mapped to as SEHException 

    Please check this link

    http://social.msdn.microsoft.com/Forums/en-US/clr/thread/94bcae24-864c-4672-8778-e7bcd8cef90f/ 

    Hope this helps you...


    If this post answers your question, please click "Mark As Answer". If this post is helpful please click "Mark as Helpful".

  • 2012年4月13日 11:36
     
     

    This only moves the problem. Now I have multiple components which wants to access the singleton, and one has to initialize it. in case that one is not initialized, the others have to wait...


    Edmondo Porcu

  • 2012年4月13日 12:31
     
     
    I'm not sure whether I understand your problem. They have to wait, indeed. But where do you see a problem?
  • 2012年4月13日 12:43
     
     
    I don't understand either. No matter what, if it takes a while to instantiate the singleton then at least one thread will have to wait for that. If multiple threads all try and access the singleton simultaneously when it hasn't yet been created, they will all have to wait.