From a8c200f25aa72e4574df9aba8085ec675fe5c3fd Mon Sep 17 00:00:00 2001 From: bao-qian Date: Thu, 21 Jul 2016 00:02:13 +0100 Subject: [PATCH] simple refactoring --- .../Wox.Plugin.Program/FileChangeWatcher.cs | 23 +++++++------------ Plugins/Wox.Plugin.Program/Main.cs | 5 ++-- 2 files changed, 10 insertions(+), 18 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs b/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs index 0ab797d9c9..95853d4628 100644 --- a/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs +++ b/Plugins/Wox.Plugin.Program/FileChangeWatcher.cs @@ -1,33 +1,31 @@ using System.Collections.Generic; using System.IO; -using System.Threading; using System.Threading.Tasks; using Wox.Infrastructure.Logger; namespace Wox.Plugin.Program { - internal class FileChangeWatcher + internal static class FileChangeWatcher { - private static bool isIndexing; - private static List watchedPath = new List(); + private static readonly List WatchedPath = new List(); public static void AddWatch(string path, string[] programSuffixes, bool includingSubDirectory = true) { - if (watchedPath.Contains(path)) return; + if (WatchedPath.Contains(path)) return; if (!Directory.Exists(path)) { Log.Warn($"FileChangeWatcher: {path} doesn't exist"); return; } - watchedPath.Add(path); + WatchedPath.Add(path); foreach (string fileType in programSuffixes) { FileSystemWatcher watcher = new FileSystemWatcher { Path = path, IncludeSubdirectories = includingSubDirectory, - Filter = string.Format("*.{0}", fileType), + Filter = $"*.{fileType}", EnableRaisingEvents = true }; watcher.Changed += FileChanged; @@ -39,15 +37,10 @@ namespace Wox.Plugin.Program private static void FileChanged(object source, FileSystemEventArgs e) { - if (!isIndexing) + Task.Run(() => { - Task.Run(() => - { - Main.IndexPrograms(); - isIndexing = false; - }); - } + Main.IndexPrograms(); + }); } - } } diff --git a/Plugins/Wox.Plugin.Program/Main.cs b/Plugins/Wox.Plugin.Program/Main.cs index bb0beb7f5f..3ce9971c38 100644 --- a/Plugins/Wox.Plugin.Program/Main.cs +++ b/Plugins/Wox.Plugin.Program/Main.cs @@ -14,7 +14,7 @@ namespace Wox.Plugin.Program { public class Main : ISettingProvider, IPlugin, IPluginI18n, IContextMenu, ISavable { - private static object lockObject = new object(); + private static readonly object IndexLock = new object(); private static List _programs = new List(); private static List _sources = new List(); private static readonly Dictionary SourceTypes = new Dictionary @@ -96,8 +96,7 @@ namespace Wox.Plugin.Program public static void IndexPrograms() { - // todo why there is a lock?? - lock (lockObject) + lock (IndexLock) { var sources = DefaultProgramSources(); if (_settings.ProgramSources != null &&