Skip to content

Commit 763fd28

Browse files
yaira2hishitetsu
andauthored
Code Quality: Replace foreach with Task.WhenAll (#14657)
Co-authored-by: hishitetsu <[email protected]>
1 parent 6835f2b commit 763fd28

File tree

6 files changed

+14
-35
lines changed

6 files changed

+14
-35
lines changed

src/Files.App/Actions/Content/ImageManipulation/BaseRotateAction.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public BaseRotateAction()
3333

3434
public async Task ExecuteAsync()
3535
{
36-
foreach (var image in context.SelectedItems)
37-
await BitmapHelper.RotateAsync(PathNormalization.NormalizePath(image.ItemPath), Rotation);
36+
await Task.WhenAll(context.SelectedItems.Select(image => BitmapHelper.RotateAsync(PathNormalization.NormalizePath(image.ItemPath), Rotation)));
3837

3938
context.ShellPage?.SlimContentPage?.ItemManipulationModel?.RefreshItemsThumbnail();
4039

src/Files.App/Actions/Content/Install/InstallInfDriverAction.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,7 @@ public InstallInfDriverAction()
3333

3434
public async Task ExecuteAsync()
3535
{
36-
foreach (ListedItem selectedItem in context.SelectedItems)
37-
await Win32API.InstallInf(selectedItem.ItemPath);
36+
await Task.WhenAll(context.SelectedItems.Select(selectedItem => Win32API.InstallInf(selectedItem.ItemPath)));
3837
}
3938

4039
public void Context_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)

src/Files.App/Data/Factories/ShellContextFlyoutHelper.cs

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
using Vanara.PInvoke;
1313
using Windows.System;
1414
using Windows.UI.Core;
15+
using static Vanara.PInvoke.Kernel32;
1516

1617
namespace Files.App.Helpers
1718
{
@@ -180,17 +181,11 @@ async Task InvokeShellMenuItemAsync(ContextMenu contextMenu, object? tag)
180181
switch (verb)
181182
{
182183
case "install" when isFont:
183-
{
184-
foreach (string path in contextMenu.ItemsPath)
185-
await Win32API.InstallFont(path, false);
186-
}
184+
await Win32API.InstallFontsAsync(contextMenu.ItemsPath.ToArray(), false);
187185
break;
188186

189187
case "installAllUsers" when isFont:
190-
{
191-
foreach (string path in contextMenu.ItemsPath)
192-
await Win32API.InstallFont(path, true);
193-
}
188+
await Win32API.InstallFontsAsync(contextMenu.ItemsPath.ToArray(), true);
194189
break;
195190

196191
case "mount":

src/Files.App/Utils/Shell/Win32API.cs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -850,21 +850,6 @@ public static async Task<bool> InstallInf(string filePath)
850850
}
851851
}
852852

853-
public static Task InstallFont(string fontFilePath, bool forAllUsers)
854-
{
855-
string fontDirectory = forAllUsers
856-
? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "Fonts")
857-
: Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "Microsoft", "Windows", "Fonts");
858-
859-
string registryKey = forAllUsers
860-
? "HKLM:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts"
861-
: "HKCU:\\Software\\Microsoft\\Windows NT\\CurrentVersion\\Fonts";
862-
863-
var destinationPath = Path.Combine(fontDirectory, Path.GetFileName(fontFilePath));
864-
865-
return RunPowershellCommandAsync($"-command \"Copy-Item '{fontFilePath}' '{fontDirectory}'; New-ItemProperty -Name '{Path.GetFileNameWithoutExtension(fontFilePath)}' -Path '{registryKey}' -PropertyType string -Value '{destinationPath}'\"", forAllUsers);
866-
}
867-
868853
public static async Task InstallFontsAsync(string[] fontFilePaths, bool forAllUsers)
869854
{
870855
string fontDirectory = forAllUsers
@@ -885,7 +870,7 @@ public static async Task InstallFontsAsync(string[] fontFilePaths, bool forAllUs
885870
if (psCommand.Length + appendCommand.Length > 32766)
886871
{
887872
// The command is too long to run at once, so run the command once up to this point.
888-
await RunPowershellCommandAsync(psCommand.Append("\"").ToString(), forAllUsers);
873+
await RunPowershellCommandAsync(psCommand.Append("\"").ToString(), true);
889874
psCommand.Clear().Append("-command \"");
890875
}
891876

src/Files.App/Views/Layouts/DetailsLayoutPage.xaml.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -431,12 +431,15 @@ private async Task ReloadItemIconsAsync()
431431

432432
ParentShellPageInstance.FilesystemViewModel.CancelExtendedPropertiesLoading();
433433
var filesAndFolders = ParentShellPageInstance.FilesystemViewModel.FilesAndFolders.ToList();
434-
foreach (ListedItem listedItem in filesAndFolders)
434+
435+
await Task.WhenAll(filesAndFolders.Select(listedItem =>
435436
{
436437
listedItem.ItemPropertiesInitialized = false;
437438
if (FileList.ContainerFromItem(listedItem) is not null)
438-
await ParentShellPageInstance.FilesystemViewModel.LoadExtendedItemPropertiesAsync(listedItem, currentIconSize);
439-
}
439+
return ParentShellPageInstance.FilesystemViewModel.LoadExtendedItemPropertiesAsync(listedItem, currentIconSize);
440+
else
441+
return Task.CompletedTask;
442+
}));
440443

441444
if (ParentShellPageInstance.FilesystemViewModel.EnabledGitProperties is not GitProperties.None)
442445
{

src/Files.Core/Services/SizeProvider/DrivesSizeProvider.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,12 @@ public async Task CleanAsync()
2424
foreach (var oldDriveName in oldDriveNames)
2525
providers.TryRemove(oldDriveName, out _);
2626

27-
foreach (var provider in providers.Values)
28-
await provider.CleanAsync();
27+
await Task.WhenAll(providers.Values.Select(provider => provider.CleanAsync()));
2928
}
3029

3130
public async Task ClearAsync()
3231
{
33-
foreach (var provider in providers.Values)
34-
await provider.ClearAsync();
32+
await Task.WhenAll(providers.Values.Select(provider => provider.ClearAsync()));
3533

3634
providers.Clear();
3735
}

0 commit comments

Comments
 (0)