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

  • Home
  • SEARCH
  • 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 8566311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:37:35+00:00 2026-06-11T17:37:35+00:00

I have a Singleton Class which loads some data on its construction. The problem

  • 0

I have a Singleton Class which loads some data on its construction. The problem is that loading this data requires calling async methods, but the constructor cannot be async.

In other words, my class has following structure:

public class Singleton
{
   private static Singleton instance;

   private Singleton() 
   {
       LoadData();
   }

   public static Singleton Instance
   {
      get 
      {
         if (instance == null)
         {
            instance = new Singleton();
         }
         return instance;
       }
    }
}

LoadData() is an async function which calls lots of async functions as well as initialization.
How can I call LoadData() properly so everything initialize correctly?

  • 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-11T17:37:36+00:00Added an answer on June 11, 2026 at 5:37 pm

    The problem is that loading this data requires calling async methods, but the constructor cannot be async.

    While you can’t make the constructor itself asynchronous, you can call asynchronous methods from within the constructor. You just will not get the results back immediately.

    Provided the asynchronous methods return Task or Task<T>, you can always use a continuation on the task to set your data within the class once the asynchronous operation completes, or just block on the results, depending on what makes the most sense in your scenario. Without knowing the requirements for construction of this object, it’s difficult to know what is appropriate in this scenario.


    Edit:

    One option, given the goals listed above, would be to change your Singleton declaration so that method to retrieve the Instance was a method, not a property. This would allow you to make it asynchronous:

    public class Singleton
    {
       private static Singleton instance;
    
       private Singleton() 
       {
              // Don't load the data here - will be called separately
       }
    
       public static async Task<Singleton> GetInstance()
       {
             if (instance == null)
             {
                instance = new Singleton();
                await instance.LoadData();
             }
    
             return instance;
        }
    }
    

    This would allow you to use await on the call to actually retrieve the instance. The nice thing about this is that it does make it very clear that you’re calling an asynchronous operation, and you will get proper handling of the results, as the result will come back like any other async method.

    Be aware, however, that this isn’t thread safe (though the original wasn’t either), so if you’re going to use this Singleton from multiple threads, you may have to rethink the overall design.

    The other option would be to make your Singleton class not automatically load data. Make the methods that retrieve the data from the class asynchronous, instead. This provides some real advantages, as the usage is probably a bit more standard, and you can support calls from multiple threads a bit more easily (since you can control the data loading process) than you’d be able to handle it with making the access of the class instance asynchronous.

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

Sidebar

Related Questions

I have a singleton class which manages data and database handling of my application.
I have a singleton class which is being used throughout the app. I am
I have a class which, by design, must follow the singleton pattern. So I
I have a Singleton which has an NSMutableArray containing a Class, the Class contains
I have a class XYZ which extends Thread and it is also a singleton
I have developed an utility class for spring which is a singleton which will
I have a singleton class for global access to config information. This singleton class
I have created a singleton class to keep track of my data on my
We have a concrete singleton service which implements Ninject.IInitializable and 2 interfaces. Problem is
I have a singleton class that I am using as part of a CAPTCHA

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.