Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7755733
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T12:38:18+00:00 2026-06-01T12:38:18+00:00

I am trying to create a singleton which has a non-empty constructor and to

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T12:38:19+00:00Added an answer on June 1, 2026 at 12:38 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Basically, what I am trying to do is to create a Singleton instance of
I am trying to access the singleton instance created by my WCF service but
I'm trying create a bot which automatically likes Facebook posts. Using Mechanize I can
I am trying create a delegate representation of constructor by emitting a Dynamic Method,
I am currently trying to figure out what the best way is to create
I have a class Cache which is quite expensive to create, but after that
I'm trying to create a service that should be a singleton - I need
I am trying to create a singleton User class in my app, here's the
I'm aiming to create a set of objects, each of which has a unique
I am trying to create a static property, and the only way I could

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.