Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Threading.Tasks;
- namespace TestProject
- {
- public class MyClass // cant implement IAsyncInitialization, since 'Initialization' property should be private
- {
- #region Fields
- // 'eagerly initialized'
- private static readonly MyClass _instance = new MyClass ();
- #endregion
- #region Properties
- #region IAsyncInitialization Implementation
- // private instead of public
- private Task Initialization { get; set; }
- #endregion
- #endregion
- #region Constructors
- private MyClass ()
- {
- Initialization = InitializeAsync();
- }
- #endregion
- #region Methods
- /// <summary>
- /// The Singleton instance
- /// </summary>
- public static MyClass Instance()
- {
- // Would this be 'await'ed everytime one calls 'MyClass.Instance()' ?
- // await _instance.Initialization; // It would also change the method to 'public static async Task<MyClass> Instance()'
- return _instance;
- }
- private async Task InitializeAsync()
- {
- // Call async method
- await DoSomethingAsync();
- // Do our own initialization (synchronous or asynchronous).
- await Task.Delay(100);
- }
- private async Task DoSomethingAsync()
- {
- // await Something();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement