title: Common Pattern to Initialize A Singleton On A Background Thread
body: <p>I currently have a singleton, which can take up to 10 seconds to initialize. However, I don't want my users to penalized (waiting) for this initialize, so I'd rather bootstrap this component on a background thread during application startup. Here's what I have:</p>
  
  <p>Singleton:</p>
  
  <pre><code>public class MySingleton
  {
      private static MySingleton _instance;
      private static readonly object _locker = new object();
  
      private MySingleton() 
      {
          Init();
      }
      public static MySingleton Instance 
      { 
          if(_instance == null) _instance = new MySingleton();
          return _instance;
      }
      public void Init()
      {
          lock(_locker)
          {
              if(_instance != null) return;
  
              // long running code here...
          }
      }
  }
  </code></pre>
  
  <p>Application Startup:</p>
  
  <pre><code>Task.Factory.StartNew(() =&gt; MySingleton.Instance.Init());
  </code></pre>
  
  <p>This code does work, guards against double init, guards against the edge case of the user needing it before it's done initializing and also guards against someone forgetting to call Init(). </p>
  
  <p>However, it feels a little clunky for two reasons:
  a) I'm going to go into the Init method twice on startup.
  b) I'd like to do threading inside the singleton, but something has to initiate the initialization.</p>
  
  <p>Is there a cleaner/nicer/better way to handle this?</p>
  
  <p>Thanks in advance for everyone's help.</p>
  
  <p>**EDIT: As pointed out in the comments, Init was mistakenly scoped as private. It should be public and has been corrected. </p>