From 1ae2666d12a782897dceca09c5ef78368cb79c88 Mon Sep 17 00:00:00 2001 From: "ivan.haidov" Date: Sun, 26 Jan 2025 15:07:59 +0100 Subject: [PATCH] Enhance cancellation handling in methods Added and propagated CancellationToken in methods to improve operation responsiveness --- .../Data/Items/WidgetFileTagsContainerItem.cs | 2 +- .../Operations/FilesystemOperations.cs | 24 +++++++++---------- .../Utils/Storage/Search/FolderSearch.cs | 2 +- .../Storage/StorageItems/NativeStorageFile.cs | 8 +++---- .../StorageItems/StreamWithContentType.cs | 2 +- .../Storage/StorageItems/SystemStorageFile.cs | 16 ++++++------- .../StorageItems/VirtualStorageFile.cs | 4 ++-- 7 files changed, 29 insertions(+), 29 deletions(-) diff --git a/src/Files.App/Data/Items/WidgetFileTagsContainerItem.cs b/src/Files.App/Data/Items/WidgetFileTagsContainerItem.cs index acf5f31cea4d..b98135d6a685 100644 --- a/src/Files.App/Data/Items/WidgetFileTagsContainerItem.cs +++ b/src/Files.App/Data/Items/WidgetFileTagsContainerItem.cs @@ -57,7 +57,7 @@ public WidgetFileTagsContainerItem(string tagUid) /// public async Task InitAsync(CancellationToken cancellationToken = default) { - await foreach (var item in FileTagsService.GetItemsForTagAsync(_tagUid)) + await foreach (var item in FileTagsService.GetItemsForTagAsync(_tagUid, cancellationToken)) { var icon = await ImageService.GetIconAsync(item.Storable, default); Tags.Add(new(item.Storable, icon)); diff --git a/src/Files.App/Utils/Storage/Operations/FilesystemOperations.cs b/src/Files.App/Utils/Storage/Operations/FilesystemOperations.cs index dbf8dbeb0fae..667a943ac8be 100644 --- a/src/Files.App/Utils/Storage/Operations/FilesystemOperations.cs +++ b/src/Files.App/Utils/Storage/Operations/FilesystemOperations.cs @@ -166,7 +166,7 @@ await DialogDisplayHelper.ShowDialogAsync( { // CopyFileFromApp only works on file not directories var fsSourceFolder = await source.ToStorageItemResult(); - var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); var fsResult = (FilesystemResult)(fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode); if (fsResult) @@ -219,7 +219,7 @@ await DialogDisplayHelper.ShowDialogAsync( { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); - FilesystemResult destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + FilesystemResult destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); var sourceResult = await source.ToStorageItemResult(); fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode; @@ -373,7 +373,7 @@ await DialogDisplayHelper.ShowDialogAsync( Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); var fsSourceFolder = await source.ToStorageItemResult(); - var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + var fsDestinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); fsResult = fsSourceFolder.ErrorCode | fsDestinationFolder.ErrorCode; if (fsResult) @@ -432,7 +432,7 @@ await DialogDisplayHelper.ShowDialogAsync( { Debug.WriteLine(System.Runtime.InteropServices.Marshal.GetLastWin32Error()); - FilesystemResult destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + FilesystemResult destinationResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); var sourceResult = await source.ToStorageItemResult(); fsResult = sourceResult.ErrorCode | destinationResult.ErrorCode; @@ -512,12 +512,12 @@ public async Task DeleteAsync(IStorageItemWithPath source, IPro { if (source.ItemType == FilesystemItemType.File) { - fsResult = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path) + fsResult = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path, cancellationToken) .OnSuccess((t) => t.DeleteAsync(permanently ? StorageDeleteOption.PermanentDelete : StorageDeleteOption.Default).AsTask()); } else if (source.ItemType == FilesystemItemType.Directory) { - fsResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path) + fsResult = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path, cancellationToken) .OnSuccess((t) => t.DeleteAsync(permanently ? StorageDeleteOption.PermanentDelete : StorageDeleteOption.Default).AsTask()); } } @@ -539,7 +539,7 @@ public async Task DeleteAsync(IStorageItemWithPath source, IPro // Recycle bin also stores a file starting with $I for each item string iFilePath = Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileName(source.Path).Replace("$R", "$I", StringComparison.Ordinal)); - await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath) + await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath, cancellationToken) .OnSuccess(iFile => iFile.DeleteAsync(StorageDeleteOption.PermanentDelete).AsTask()); } fsProgress.ReportStatus(fsResult); @@ -738,8 +738,8 @@ public async Task RestoreFromTrashAsync(IStorageItemWithPath so { if (source.ItemType == FilesystemItemType.Directory) { - FilesystemResult sourceFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path); - FilesystemResult destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + FilesystemResult sourceFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(source.Path, cancellationToken); + FilesystemResult destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); fsResult = sourceFolder.ErrorCode | destinationFolder.ErrorCode; fsProgress.ReportStatus(fsResult); @@ -759,8 +759,8 @@ public async Task RestoreFromTrashAsync(IStorageItemWithPath so } else { - FilesystemResult sourceFile = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path); - FilesystemResult destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination)); + FilesystemResult sourceFile = await _associatedInstance.ShellViewModel.GetFileFromPathAsync(source.Path, cancellationToken); + FilesystemResult destinationFolder = await _associatedInstance.ShellViewModel.GetFolderFromPathAsync(PathNormalization.GetParentDir(destination), cancellationToken); fsResult = sourceFile.ErrorCode | destinationFolder.ErrorCode; fsProgress.ReportStatus(fsResult); @@ -782,7 +782,7 @@ public async Task RestoreFromTrashAsync(IStorageItemWithPath so // Recycle bin also stores a file starting with $I for each item string iFilePath = Path.Combine(Path.GetDirectoryName(source.Path), Path.GetFileName(source.Path).Replace("$R", "$I", StringComparison.Ordinal)); - await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath) + await _associatedInstance.ShellViewModel.GetFileFromPathAsync(iFilePath, cancellationToken) .OnSuccess(iFile => iFile.DeleteAsync(StorageDeleteOption.PermanentDelete).AsTask()); } diff --git a/src/Files.App/Utils/Storage/Search/FolderSearch.cs b/src/Files.App/Utils/Storage/Search/FolderSearch.cs index 487352966adc..ff2b7e801daf 100644 --- a/src/Files.App/Utils/Storage/Search/FolderSearch.cs +++ b/src/Files.App/Utils/Storage/Search/FolderSearch.cs @@ -336,7 +336,7 @@ await Task.Run(() => } while (hasNextFile); Win32PInvoke.FindClose(hFile); - }); + }, token); } } diff --git a/src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs b/src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs index ac855ca4a107..4ced72edb2e4 100644 --- a/src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs +++ b/src/Files.App/Utils/Storage/StorageItems/NativeStorageFile.cs @@ -86,8 +86,8 @@ public override IAsyncOperation CopyAsync(IStorageFolder destin using (var inStream = await this.OpenStreamForReadAsync()) using (var outStream = await destFile.OpenStreamForWriteAsync()) { - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); } } return destFile; @@ -251,8 +251,8 @@ public override IAsyncAction RenameAsync(string desiredName, NameCollisionOption using (var inStream = await this.OpenStreamForReadAsync()) using (var outStream = await destFile.OpenStreamForWriteAsync()) { - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); } await DeleteAsync(); } diff --git a/src/Files.App/Utils/Storage/StorageItems/StreamWithContentType.cs b/src/Files.App/Utils/Storage/StorageItems/StreamWithContentType.cs index d1971d9fe17d..2c2e74012d4a 100644 --- a/src/Files.App/Utils/Storage/StorageItems/StreamWithContentType.cs +++ b/src/Files.App/Utils/Storage/StorageItems/StreamWithContentType.cs @@ -117,7 +117,7 @@ public IAsyncOperation FlushAsync() return AsyncInfo.Run(async (cancellationToken) => { - await stream.FlushAsync(); + await stream.FlushAsync(cancellationToken); return true; }); } diff --git a/src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs b/src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs index a836bde6cd4e..250b916f2727 100644 --- a/src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs +++ b/src/Files.App/Utils/Storage/StorageItems/SystemStorageFile.cs @@ -78,8 +78,8 @@ public override IAsyncOperation CopyAsync(IStorageFolder destin using (var inStream = await this.OpenStreamForReadAsync()) using (var outStream = await destFile.OpenStreamForWriteAsync()) { - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); } return destFile; } @@ -96,8 +96,8 @@ public override IAsyncOperation CopyAsync(IStorageFolder destin using (var inStream = await this.OpenStreamForReadAsync()) using (var outStream = new FileStream(hFile, FileAccess.Write)) { - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); } return new NativeStorageFile(destination, desiredNewName, DateTime.Now); } @@ -143,8 +143,8 @@ public override IAsyncAction CopyAndReplaceAsync(IStorageFile fileToReplace) using var inStream = await this.OpenStreamForReadAsync(); using var outStream = await fileToReplace.OpenStreamForWriteAsync(); - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); }); } public override IAsyncAction MoveAndReplaceAsync(IStorageFile fileToReplace) @@ -154,8 +154,8 @@ public override IAsyncAction MoveAndReplaceAsync(IStorageFile fileToReplace) using var inStream = await this.OpenStreamForReadAsync(); using var outStream = await fileToReplace.OpenStreamForWriteAsync(); - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); // Move unsupported, copy but do not delete original }); } diff --git a/src/Files.App/Utils/Storage/StorageItems/VirtualStorageFile.cs b/src/Files.App/Utils/Storage/StorageItems/VirtualStorageFile.cs index 06f9032f3ab0..e2eaf1035a40 100644 --- a/src/Files.App/Utils/Storage/StorageItems/VirtualStorageFile.cs +++ b/src/Files.App/Utils/Storage/StorageItems/VirtualStorageFile.cs @@ -128,8 +128,8 @@ public override IAsyncOperation CopyAsync(IStorageFolder destin using (var inStream = await this.OpenStreamForReadAsync()) using (var outStream = await destFile.OpenStreamForWriteAsync()) { - await inStream.CopyToAsync(outStream); - await outStream.FlushAsync(); + await inStream.CopyToAsync(outStream, cancellationToken); + await outStream.FlushAsync(cancellationToken); } return destFile; }