public Task UnloadAsync() { IMultiLifetimeInstance instance = null; lock (_lock) { if (_instance != null) { instance = _instance; _instance = null; _loadedSource = new TaskCompletionSource <object>(); } } if (instance != null) { return(instance.DisposeAsync()); } return(Task.CompletedTask); }
private async Task LoadCoreAsync() { TaskCompletionSource <object> loadedSource = null; IMultiLifetimeInstance instance; lock (_lock) { if (_instance == null) { _instance = CreateInstance(); loadedSource = _loadedSource; } instance = _instance; } // While all callers should wait on InitializeAsync, // only one should complete the completion source await instance.InitializeAsync(); loadedSource?.SetResult(null); }