mirror of
https://github.com/microsoft/PowerToys.git
synced 2026-04-06 19:26:39 +02:00
Refactoring multithreading
1. ThreadPool -> Task 2. fix deadlock 3. remove unnecessory application.dispatcher.invoke 4. enable non-main thread access to results collection 5. Misc 6. part of #412
This commit is contained in:
@@ -4,6 +4,7 @@ using System.Diagnostics;
|
||||
using System.IO;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Infrastructure.Exception;
|
||||
@@ -55,17 +56,14 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
else
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem(state =>
|
||||
string actionReponse = ExecuteCallback(result1.JsonRPCAction);
|
||||
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
|
||||
if (jsonRpcRequestModel != null
|
||||
&& !String.IsNullOrEmpty(jsonRpcRequestModel.Method)
|
||||
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
|
||||
{
|
||||
string actionReponse = ExecuteCallback(result1.JsonRPCAction);
|
||||
JsonRPCRequestModel jsonRpcRequestModel = JsonConvert.DeserializeObject<JsonRPCRequestModel>(actionReponse);
|
||||
if (jsonRpcRequestModel != null
|
||||
&& !String.IsNullOrEmpty(jsonRpcRequestModel.Method)
|
||||
&& jsonRpcRequestModel.Method.StartsWith("Wox."))
|
||||
{
|
||||
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
|
||||
}
|
||||
});
|
||||
ExecuteWoxAPI(jsonRpcRequestModel.Method.Substring(4), jsonRpcRequestModel.Parameters);
|
||||
}
|
||||
}
|
||||
}
|
||||
return !result1.JsonRPCAction.DontHideAfterAction;
|
||||
@@ -126,7 +124,7 @@ namespace Wox.Core.Plugin
|
||||
{
|
||||
using (Process process = Process.Start(startInfo))
|
||||
{
|
||||
if (process != null)
|
||||
if (process != null)
|
||||
{
|
||||
using (StreamReader reader = process.StandardOutput)
|
||||
{
|
||||
@@ -152,7 +150,7 @@ namespace Wox.Core.Plugin
|
||||
}
|
||||
}
|
||||
}
|
||||
catch(Exception e)
|
||||
catch (Exception e)
|
||||
{
|
||||
throw new WoxJsonRPCException(e.Message);
|
||||
}
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.IO;
|
||||
using System.Threading.Tasks;
|
||||
using Newtonsoft.Json;
|
||||
using Wox.Infrastructure.Exception;
|
||||
using Wox.Infrastructure.Logger;
|
||||
@@ -11,8 +13,8 @@ namespace Wox.Core.Plugin
|
||||
|
||||
internal abstract class PluginConfig
|
||||
{
|
||||
private const string pluginConfigName = "plugin.json";
|
||||
private static List<PluginMetadata> pluginMetadatas = new List<PluginMetadata>();
|
||||
private const string PluginConfigName = "plugin.json";
|
||||
private static readonly List<PluginMetadata> PluginMetadatas = new List<PluginMetadata>();
|
||||
|
||||
/// <summary>
|
||||
/// Parse plugin metadata in giving directories
|
||||
@@ -21,45 +23,41 @@ namespace Wox.Core.Plugin
|
||||
/// <returns></returns>
|
||||
public static List<PluginMetadata> Parse(string[] pluginDirectories)
|
||||
{
|
||||
pluginMetadatas.Clear();
|
||||
foreach (string pluginDirectory in pluginDirectories)
|
||||
{
|
||||
ParsePluginConfigs(pluginDirectory);
|
||||
}
|
||||
|
||||
return pluginMetadatas;
|
||||
PluginMetadatas.Clear();
|
||||
var directories = pluginDirectories.SelectMany(Directory.GetDirectories);
|
||||
ParsePluginConfigs(directories);
|
||||
return PluginMetadatas;
|
||||
}
|
||||
|
||||
private static void ParsePluginConfigs(string pluginDirectory)
|
||||
private static void ParsePluginConfigs(IEnumerable<string> directories)
|
||||
{
|
||||
if (!Directory.Exists(pluginDirectory)) return;
|
||||
|
||||
string[] directories = Directory.GetDirectories(pluginDirectory);
|
||||
foreach (string directory in directories)
|
||||
Parallel.ForEach(directories, directory =>
|
||||
{
|
||||
if (File.Exists((Path.Combine(directory, "NeedDelete.txt"))))
|
||||
if (File.Exists(Path.Combine(directory, "NeedDelete.txt")))
|
||||
{
|
||||
try
|
||||
{
|
||||
Directory.Delete(directory, true);
|
||||
continue;
|
||||
}
|
||||
catch (Exception e)
|
||||
{
|
||||
Log.Fatal(e);
|
||||
}
|
||||
}
|
||||
PluginMetadata metadata = GetPluginMetadata(directory);
|
||||
if (metadata != null)
|
||||
else
|
||||
{
|
||||
pluginMetadatas.Add(metadata);
|
||||
PluginMetadata metadata = GetPluginMetadata(directory);
|
||||
if (metadata != null)
|
||||
{
|
||||
PluginMetadatas.Add(metadata);
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
private static PluginMetadata GetPluginMetadata(string pluginDirectory)
|
||||
{
|
||||
string configPath = Path.Combine(pluginDirectory, pluginConfigName);
|
||||
string configPath = Path.Combine(pluginDirectory, PluginConfigName);
|
||||
if (!File.Exists(configPath))
|
||||
{
|
||||
Log.Warn($"parse plugin {configPath} failed: didn't find config file.");
|
||||
|
||||
@@ -3,6 +3,7 @@ using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using Wox.Core.Resource;
|
||||
using Wox.Core.UserSettings;
|
||||
using Wox.Infrastructure;
|
||||
@@ -31,7 +32,7 @@ namespace Wox.Core.Plugin
|
||||
public static IPublicAPI API { private set; get; }
|
||||
private static PluginsSettings _settings;
|
||||
private static List<PluginMetadata> _metadatas;
|
||||
private static readonly string[] Directories = {Infrastructure.Wox.PreinstalledDirectory, Infrastructure.Wox.UserDirectory };
|
||||
private static readonly string[] Directories = { Infrastructure.Wox.PreinstalledDirectory, Infrastructure.Wox.UserDirectory };
|
||||
|
||||
private static void ValidateUserDirectory()
|
||||
{
|
||||
@@ -69,43 +70,36 @@ namespace Wox.Core.Plugin
|
||||
ResourceMerger.UpdatePluginLanguages();
|
||||
|
||||
API = api;
|
||||
foreach (PluginPair pluginPair in AllPlugins)
|
||||
Parallel.ForEach(AllPlugins, pair =>
|
||||
{
|
||||
PluginPair pair = pluginPair;
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
var milliseconds = Stopwatch.Normal($"Plugin init: {pair.Metadata.Name}", () =>
|
||||
{
|
||||
var milliseconds = Stopwatch.Normal($"Plugin init: {pair.Metadata.Name}", () =>
|
||||
pair.Plugin.Init(new PluginInitContext
|
||||
{
|
||||
pair.Plugin.Init(new PluginInitContext
|
||||
{
|
||||
CurrentPluginMetadata = pair.Metadata,
|
||||
Proxy = HttpProxy.Instance,
|
||||
API = API
|
||||
});
|
||||
CurrentPluginMetadata = pair.Metadata,
|
||||
Proxy = HttpProxy.Instance,
|
||||
API = API
|
||||
});
|
||||
pair.InitTime = milliseconds;
|
||||
InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair);
|
||||
});
|
||||
}
|
||||
pair.InitTime = milliseconds;
|
||||
InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair);
|
||||
});
|
||||
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
|
||||
foreach (var plugin in AllPlugins)
|
||||
{
|
||||
_contextMenuPlugins = GetPluginsForInterface<IContextMenu>();
|
||||
foreach (var plugin in AllPlugins)
|
||||
if (IsGlobalPlugin(plugin.Metadata))
|
||||
{
|
||||
if (IsGlobalPlugin(plugin.Metadata))
|
||||
GlobalPlugins.Add(plugin);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string actionKeyword in plugin.Metadata.ActionKeywords)
|
||||
{
|
||||
GlobalPlugins.Add(plugin);
|
||||
}
|
||||
else
|
||||
{
|
||||
foreach (string actionKeyword in plugin.Metadata.ActionKeywords)
|
||||
{
|
||||
NonGlobalPlugins[actionKeyword] = plugin;
|
||||
}
|
||||
NonGlobalPlugins[actionKeyword] = plugin;
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -15,7 +15,7 @@ namespace Wox.Core.Plugin
|
||||
public const string Python = "python";
|
||||
public const string PythonExecutable = "pythonw.exe";
|
||||
|
||||
public static IEnumerable<PluginPair> CSharpPlugins(IEnumerable<PluginMetadata> source)
|
||||
public static IEnumerable<PluginPair> CSharpPlugins(List<PluginMetadata> source)
|
||||
{
|
||||
var plugins = new List<PluginPair>();
|
||||
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.CSharp);
|
||||
@@ -63,7 +63,7 @@ namespace Wox.Core.Plugin
|
||||
return plugins;
|
||||
}
|
||||
|
||||
public static IEnumerable<PluginPair> PythonPlugins(IEnumerable<PluginMetadata> source, string pythonDirecotry)
|
||||
public static IEnumerable<PluginPair> PythonPlugins(List<PluginMetadata> source, string pythonDirecotry)
|
||||
{
|
||||
var metadatas = source.Where(o => o.Language.ToUpper() == AllowedLanguage.Python);
|
||||
string filename;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
using System.Collections.Generic;
|
||||
using System.Reflection;
|
||||
using System.Threading;
|
||||
using System.Threading.Tasks;
|
||||
using System.Windows.Forms;
|
||||
using NAppUpdate.Framework;
|
||||
using NAppUpdate.Framework.Common;
|
||||
@@ -80,7 +81,7 @@ namespace Wox.Core.Updater
|
||||
|
||||
public void CheckUpdate()
|
||||
{
|
||||
ThreadPool.QueueUserWorkItem(o =>
|
||||
Task.Run(() =>
|
||||
{
|
||||
string json = HttpRequest.Get(VersionCheckURL, HttpProxy.Instance);
|
||||
if (!string.IsNullOrEmpty(json))
|
||||
|
||||
Reference in New Issue
Block a user