public Task InitializeAsync() { if (watchers.Count == 0) { log.Debug("Initializing in directory '{0}'.", directoryPath); Action initializeFromDirectory = () => { log.Debug("Initializing from file system."); using (backgroundContext.Start()) { storage.AddRange(EnumerateDirectory(directoryPath)); storage.IsCacheUsed = false; } log.Info("Found '{0}' items in file system.", storage.Count); }; if (!storage.IsCacheEmpty) { log.Debug("Cache is not empty."); if (storage.Count == 0) { log.Debug("Cache is not yet initialized."); } else { log.Debug("Cache contains '{0}' items.", storage.Count); } Task.Factory.StartNew(initializeFromDirectory).ContinueWith(t => { if (t.IsFaulted) { backgroundExceptionHandler.Handle(t.Exception); } }); } return(Task.Factory.StartNew(() => { if (watchers.Count == 0) { InitializeWatchers(directoryPath); if (storage.IsCacheEmpty) { log.Debug("Cache is empty."); initializeFromDirectory(); } } })); } return(Async.CompletedTask); }