simple refactoring

This commit is contained in:
bao-qian
2016-07-21 00:02:13 +01:00
parent 59fbb2acb1
commit a8c200f25a
2 changed files with 10 additions and 18 deletions

View File

@@ -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<string> watchedPath = new List<string>();
private static readonly List<string> WatchedPath = new List<string>();
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();
});
}
}
}

View File

@@ -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<Program> _programs = new List<Program>();
private static List<IProgramSource> _sources = new List<IProgramSource>();
private static readonly Dictionary<string, Type> SourceTypes = new Dictionary<string, Type>
@@ -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 &&