public void Load() { lock (_lock) { _settings.Clear(); var path = PathUtility.DataPath.CombineWithFilePath(new FilePath("settings.dat")); if (_fileSystem.Exist(path)) { try { using (var stream = _fileSystem.GetFile(path).OpenRead()) using (var reader = new BinaryReader(stream)) { var count = reader.ReadInt32(); for (var index = 0; index < count; index++) { var key = reader.ReadString(); var value = reader.ReadString(); Set(key, value); } } _log.Information($"Loaded settings from '{path.FullPath}'."); } catch (Exception ex) { _log.Error(ex, $"An error occured while loading settings from '{path.FullPath}'."); } } } }
public Task Start(CancellationTokenSource source) { _log.Information($"Starting {Name.ToLowerInvariant()}..."); _source = source; Task = Task.Factory.StartNew( async() => { try { _log.Information($"{Name} started."); if (!await _worker.Run(_source.Token)) { _source.Cancel(); } } catch (OperationCanceledException) { _log.Information($"{Name} aborted."); } catch (Exception ex) { _log.Error($"{Name}: {ex.Message} ({ex.GetType().FullName})"); _source.Cancel(); } finally { _log.Information($"{Name} stopped."); } }, TaskCreationOptions.LongRunning); return(Task); }
public void Stop() { if (!_source.IsCancellationRequested) { _log.Information("We were instructed to stop."); _source.Cancel(); } }
public void Load() { lock (_lock) { _settings.Clear(); var path = PathUtility.DataPath.CombineWithFilePath(new FilePath("settings.dat")); if (_fileSystem.Exist(path)) { try { using (var stream = _fileSystem.GetFile(path).OpenRead()) using (var reader = new BinaryReader(stream)) { var count = reader.ReadInt32(); for (var index = 0; index < count; index++) { var key = reader.ReadString(); var value = reader.ReadString(); Set(key, value); } } _log.Information($"Loaded settings from '{path.FullPath}'."); } catch (Exception ex) { _log.Error(ex, $"An error occured while loading settings from '{path.FullPath}'."); } } else { // Set the default settings. this.Set(Constants.Settings.General.CheckForUpdates, true); this.Set(Constants.Settings.General.IncludePreviews, false); // Save the default settings. Save(); } } }
public async Task <bool> Run(CancellationToken token) { #if !DEBUG || FAKERELEASE // Wait a minute after the application starts to check for updates. if (token.WaitHandle.WaitOne((int)TimeSpan.FromSeconds(30).TotalMilliseconds)) { _log.Information("We were instructed to stop (1)."); return(true); } #endif // Should we even check for updates? if (!_settings.Get <bool>(Constants.Settings.General.CheckForUpdates)) { _log.Information("We were instructed to stop since we should not check for updates."); return(true); } while (true) { _log.Information("Checking for updates..."); var result = await _checker.CheckForUpdates(); if (result != null && result.FutureVersion > result.CurrentVersion) { _log.Information($"New version available: {result.FutureVersion}"); _eventAggregator.PublishOnUIThread(new UpdateAvailableMessage(result)); // Don't run this check again. // At least not until we restart. _log.Information("Shutting down since update message have been sent."); return(true); } // Sleep for 30 minutes before checking for updates again. _log.Information("No new version available."); if (token.WaitHandle.WaitOne((int)TimeSpan.FromMinutes(30).TotalMilliseconds)) { _log.Information("We were instructed to stop (2)."); return(true); } } }
public Task <bool> Run(CancellationToken token) { return(Task.Run(() => { while (true) { var indexingWatch = new Stopwatch(); indexingWatch.Start(); var result = LoadResults(token); if (token.IsCancellationRequested) { break; } _log.Debug("Updating index..."); var indexUpdateWatch = new Stopwatch(); indexUpdateWatch.Start(); var trie = new Trie <IndexedEntry>(); foreach (var file in result) { // Index individual words as well as combinations. Index(trie, file.Title, file); Index(trie, file.Description, file); // Also index the whole words without tokenization. trie.Insert(file.Title, file); // Is this a file path? if (file is IHasPath entryWithPath) { if (entryWithPath.Path is FilePath filePath) { // Index individual words as well as combinations. Index(trie, filePath.GetFilenameWithoutExtension().FullPath, file); Index(trie, filePath.GetFilename().RemoveExtension().FullPath, file); // Also index the whole words without tokenization. trie.Insert(filePath.GetFilenameWithoutExtension().FullPath, file); trie.Insert(filePath.GetFilename().FullPath, file); } } } indexUpdateWatch.Stop(); _log.Debug($"Building trie took {indexUpdateWatch.ElapsedMilliseconds}ms"); _log.Debug("Writing index..."); Interlocked.Exchange(ref _trie, trie); _log.Verbose($"Nodes: {_trie.NodeCount}"); _log.Verbose($"Items: {_trie.ItemCount}"); indexingWatch.Stop(); _log.Debug($"Indexing done. Took {indexingWatch.ElapsedMilliseconds}ms"); // Wait for a while. var index = WaitHandle.WaitAny(new[] { token.WaitHandle, _trigger }, (int)TimeSpan.FromMinutes(5).TotalMilliseconds); if (index == 0) { _log.Information("We were instructed to stop (2)."); break; } // Triggered update? if (index == 1) { _log.Information("A re-index was triggered."); _trigger.Reset(); } } return true; }, token)); }
public async Task <bool> Run(CancellationToken token) { while (true) { // Ask all sources for files. var result = new HashSet <IndexedEntry>(); foreach (var source in _sources) { if (token.WaitHandle.WaitOne((int)TimeSpan.FromMinutes(0).TotalMilliseconds)) { _log.Information("We were instructed to stop (1). Aborting indexing..."); break; } _log.Debug($"Running '{source.Name}' indexer..."); foreach (var file in source.Index()) { result.Add(file); } } _log.Debug("Updating index..."); var trie = new Trie <IndexedEntry>(); foreach (var file in result) { // Index individual words as well as combinations. Index(trie, file.Title, file); Index(trie, file.Description, file); // Also index the whole words without tokenization. trie.Insert(file.Title, file); // Is this a file path? if (file is IHasPath entryWithPath) { if (entryWithPath.Path is FilePath filePath) { // Index individual words as well as combinations. Index(trie, filePath.GetFilenameWithoutExtension().FullPath, file); Index(trie, filePath.GetFilename().RemoveExtension().FullPath, file); // Also index the whole words without tokenization. trie.Insert(filePath.GetFilenameWithoutExtension().FullPath, file); trie.Insert(filePath.GetFilename().FullPath, file); } } } _log.Debug("Writing index..."); using (await _lock.WriterLockAsync(token)) { _trie = trie; } _log.Verbose($"Nodes: {_trie.NodeCount}"); _log.Verbose($"Items: {_trie.ItemCount}"); // Wait for a minute. _log.Debug("Indexing done."); if (token.WaitHandle.WaitOne((int)TimeSpan.FromMinutes(5).TotalMilliseconds)) { _log.Information("We were instructed to stop (2)."); break; } } return(true); }