From fbb69ea587ba2cc9f8a3ae26bbdeb6792321e420 Mon Sep 17 00:00:00 2001 From: Leilei Zhang Date: Wed, 28 May 2025 17:09:57 +0800 Subject: [PATCH] use Parallel.ForEach to replace the AsParallel() --- .../Microsoft.CmdPal.Ext.Apps/Programs/UWP.cs | 53 +++++++------- .../Programs/Win32Program.cs | 72 ++++++++++++++++--- 2 files changed, 91 insertions(+), 34 deletions(-) diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWP.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWP.cs index 67f2792d8e..198264d7d9 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWP.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/UWP.cs @@ -3,9 +3,11 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.IO.Abstractions; using System.Linq; +using System.Threading.Tasks; using System.Xml.Linq; using ManagedCommon; using Microsoft.CmdPal.Ext.Apps.Utils; @@ -127,35 +129,36 @@ public partial class UWP { var windows10 = new Version(10, 0); var support = Environment.OSVersion.Version.Major >= windows10.Major; - if (support) - { - var applications = CurrentUserPackages().SelectMany(p => - { - UWP u; - try - { - u = new UWP(p); - u.InitializeAppInfo(p.InstalledLocation); - } - catch (Exception ex) - { - Logger.LogError(ex.Message); - return Array.Empty(); - } - return u.Apps; - }); - - var updatedListWithoutDisabledApps = applications - .Where(t1 => AllAppsSettings.Instance.DisabledProgramSources.All(x => x.UniqueIdentifier != t1.UniqueIdentifier)) - .Select(x => x); - - return updatedListWithoutDisabledApps.ToArray(); - } - else + if (!support) { return Array.Empty(); } + + var appsBag = new ConcurrentBag(); + + Parallel.ForEach(CurrentUserPackages(), p => + { + try + { + var u = new UWP(p); + u.InitializeAppInfo(p.InstalledLocation); + + foreach (var app in u.Apps) + { + if (AllAppsSettings.Instance.DisabledProgramSources.All(x => x.UniqueIdentifier != app.UniqueIdentifier)) + { + appsBag.Add(app); + } + } + } + catch (Exception ex) + { + Logger.LogError(ex.Message); + } + }); + + return appsBag.ToArray(); } private static IEnumerable CurrentUserPackages() diff --git a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/Win32Program.cs b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/Win32Program.cs index 0a91bf82e6..210febc549 100644 --- a/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/Win32Program.cs +++ b/src/modules/cmdpal/ext/Microsoft.CmdPal.Ext.Apps/Programs/Win32Program.cs @@ -3,6 +3,7 @@ // See the LICENSE file in the project root for more information. using System; +using System.Collections.Concurrent; using System.Collections.Generic; using System.ComponentModel.Design; using System.Diagnostics; @@ -841,16 +842,69 @@ public class Win32Program : IProgram var disabledProgramsList = settings.DisabledProgramSources; // Get all paths but exclude all normal .Executables - paths.UnionWith(sources - .SelectMany(source => source.IsEnabled ? source.GetPaths() : Enumerable.Empty()) - .Where(programPath => disabledProgramsList.All(x => x.UniqueIdentifier != programPath)) - .Where(path => !ExecutableApplicationExtensions.Contains(Extension(path)))); - runCommandPaths.UnionWith(runCommandSources - .SelectMany(source => source.IsEnabled ? source.GetPaths() : Enumerable.Empty()) - .Where(programPath => disabledProgramsList.All(x => x.UniqueIdentifier != programPath))); + var pathBag = new ConcurrentBag(); - var programs = paths.Select(source => GetProgramFromPath(source)); - var runCommandPrograms = runCommandPaths.Select(source => GetRunCommandProgramFromPath(source)); + Parallel.ForEach(sources, source => + { + if (!source.IsEnabled) + { + return; + } + + foreach (var path in source.GetPaths()) + { + if (disabledProgramsList.All(x => x.UniqueIdentifier != path) && + !ExecutableApplicationExtensions.Contains(Extension(path))) + { + pathBag.Add(path); + } + } + }); + + paths.UnionWith(pathBag); + + var runCommandPathBag = new ConcurrentBag(); + + Parallel.ForEach(runCommandSources, source => + { + if (!source.IsEnabled) + { + return; + } + + foreach (var path in source.GetPaths()) + { + if (disabledProgramsList.All(x => x.UniqueIdentifier != path)) + { + runCommandPathBag.Add(path); + } + } + }); + + runCommandPaths.UnionWith(runCommandPathBag); + + var programsList = new ConcurrentBag(); + Parallel.ForEach(paths, source => + { + var program = GetProgramFromPath(source); + if (program != null) + { + programsList.Add(program); + } + }); + + var runCommandProgramsList = new ConcurrentBag(); + Parallel.ForEach(runCommandPaths, source => + { + var program = GetRunCommandProgramFromPath(source); + if (program != null) + { + runCommandProgramsList.Add(program); + } + }); + + var programs = programsList.ToList(); + var runCommandPrograms = runCommandProgramsList.ToList(); return DeduplicatePrograms(programs.Concat(runCommandPrograms).Where(program => program?.Valid == true)); }