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
- {
- #region Fields
- // 'eagerly initialized'
- private static readonly MyClass _instance = new MyClass ();
- #endregion
- #region Properties
- private Task<MyClass> Initialization { get; set; }
- #endregion
- #region Constructors
- private MyClass ()
- {
- Initialization = InitializeAsync();
- }
- #endregion
- #region Methods
- public static MyClass Instance()
- {
- // Shouldn't I check if Initialization is null? Would that be thread safe?
- return _instance;
- }
- public static Task<MyClass> InstanceAsync()
- {
- return _instance.Initialization;
- }
- private async Task<MyClass> InitializeAsync()
- {
- // Call async method
- await DoSomethingAsync();
- // Do our own initialization (synchronous or asynchronous).
- await Task.Delay(100);
- return this;
- }
- private async Task DoSomethingAsync()
- {
- await Task.Delay(10000);
- // await Something();
- }
- #endregion
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement