Skip to content

Commit fc1b171

Browse files
committed
Run file ops on background thread
1 parent 84a270f commit fc1b171

File tree

4 files changed

+20
-23
lines changed

4 files changed

+20
-23
lines changed

src/Files/Filesystem/FilesystemOperations/FilesystemOperations.cs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -610,8 +610,6 @@ await associatedInstance.FilesystemViewModel.GetFileFromPathAsync(iFilePath)
610610

611611
if (fsResult)
612612
{
613-
await associatedInstance.FilesystemViewModel.RemoveFileOrFolderAsync(source.Path);
614-
615613
if (!permanently)
616614
{
617615
// Enumerate Recycle Bin

src/Files/Filesystem/FilesystemOperations/Helpers/FilesystemHelpers.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public FilesystemHelpers(IShellPage associatedInstance, CancellationToken cancel
7676
{
7777
this.associatedInstance = associatedInstance;
7878
this.cancellationToken = cancellationToken;
79-
this.filesystemOperations = new ShellFilesystemOperations(this.associatedInstance);
79+
this.filesystemOperations = new ShellFilesystemOperations(associatedInstance, NativeWinApiHelper.CoreWindowHandle.ToInt64());
8080
this.recycleBinHelpers = new RecycleBinHelpers();
8181
}
8282

@@ -182,7 +182,7 @@ public async Task<ReturnResult> DeleteItemsAsync(IEnumerable<IStorageItemWithPat
182182
var sw = new Stopwatch();
183183
sw.Start();
184184

185-
IStorageHistory history = await filesystemOperations.DeleteItemsAsync(source, banner.Progress, banner.ErrorCode, permanently, token);
185+
IStorageHistory history = await Task.Run(() => filesystemOperations.DeleteItemsAsync(source, banner.Progress, banner.ErrorCode, permanently, token));
186186
((IProgress<float>)banner.Progress).Report(100.0f);
187187
await Task.Yield();
188188

@@ -407,7 +407,7 @@ public async Task<ReturnResult> RestoreItemsFromTrashAsync(IEnumerable<IStorageI
407407
var sw = new Stopwatch();
408408
sw.Start();
409409

410-
IStorageHistory history = await filesystemOperations.RestoreItemsFromTrashAsync(source, destination, null, errorCode, cancellationToken);
410+
IStorageHistory history = await Task.Run(() => filesystemOperations.RestoreItemsFromTrashAsync(source, destination, null, errorCode, cancellationToken));
411411
await Task.Yield();
412412

413413
if (registerHistory && source.Any((item) => !string.IsNullOrWhiteSpace(item.Path)))
@@ -523,7 +523,7 @@ public async Task<ReturnResult> CopyItemsAsync(IEnumerable<IStorageItemWithPath>
523523

524524
itemManipulationModel?.ClearSelection();
525525

526-
IStorageHistory history = await filesystemOperations.CopyItemsAsync(source, destination, collisions, banner.Progress, banner.ErrorCode, token);
526+
IStorageHistory history = await Task.Run(() => filesystemOperations.CopyItemsAsync(source, destination, collisions, banner.Progress, banner.ErrorCode, token));
527527
((IProgress<float>)banner.Progress).Report(100.0f);
528528
await Task.Yield();
529529

@@ -741,7 +741,7 @@ public async Task<ReturnResult> MoveItemsAsync(IEnumerable<IStorageItemWithPath>
741741

742742
itemManipulationModel?.ClearSelection();
743743

744-
IStorageHistory history = await filesystemOperations.MoveItemsAsync(source, destination, collisions, banner.Progress, banner.ErrorCode, token);
744+
IStorageHistory history = await Task.Run(() => filesystemOperations.MoveItemsAsync(source, destination, collisions, banner.Progress, banner.ErrorCode, token));
745745
((IProgress<float>)banner.Progress).Report(100.0f);
746746
await Task.Yield();
747747

@@ -976,7 +976,7 @@ public async Task<ReturnResult> CreateShortcutFromClipboard(DataPackageView pack
976976
var dest = source.Select(x => Path.Combine(destination,
977977
string.Format("ShortcutCreateNewSuffix".GetLocalized(), x.Name) + ".lnk"));
978978

979-
var history = await filesystemOperations.CreateShortcutItemsAsync(source, dest, null, errorCode, cancellationToken);
979+
var history = await Task.Run(() => filesystemOperations.CreateShortcutItemsAsync(source, dest, null, errorCode, cancellationToken));
980980

981981
if (registerHistory)
982982
{

src/Files/Filesystem/FilesystemOperations/ShellFilesystemOperations.cs

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ public class ShellFilesystemOperations : IFilesystemOperations
2222

2323
private IShellPage associatedInstance;
2424

25+
private long parentWindow;
26+
2527
private FilesystemOperations filesystemOperations;
2628

2729
private RecycleBinHelpers recycleBinHelpers;
@@ -30,9 +32,10 @@ public class ShellFilesystemOperations : IFilesystemOperations
3032

3133
#region Constructor
3234

33-
public ShellFilesystemOperations(IShellPage associatedInstance)
35+
public ShellFilesystemOperations(IShellPage associatedInstance, long parentWindow)
3436
{
3537
this.associatedInstance = associatedInstance;
38+
this.parentWindow = parentWindow;
3639
filesystemOperations = new FilesystemOperations(associatedInstance);
3740
recycleBinHelpers = new RecycleBinHelpers();
3841
}
@@ -95,7 +98,7 @@ public async Task<IStorageHistory> CopyItemsAsync(IEnumerable<IStorageItemWithPa
9598
{ "filepath", string.Join('|', sourceRename.Select(s => s.Path)) },
9699
{ "destpath", string.Join('|', destinationRename) },
97100
{ "overwrite", false },
98-
{ "HWND", NativeWinApiHelper.CoreWindowHandle.ToInt64() }
101+
{ "HWND", parentWindow }
99102
});
100103
result &= (FilesystemResult)(status == AppServiceResponseStatus.Success
101104
&& response.Get("Success", false));
@@ -112,7 +115,7 @@ public async Task<IStorageHistory> CopyItemsAsync(IEnumerable<IStorageItemWithPa
112115
{ "filepath", string.Join('|', sourceReplace.Select(s => s.Path)) },
113116
{ "destpath", string.Join('|', destinationReplace) },
114117
{ "overwrite", true },
115-
{ "HWND", NativeWinApiHelper.CoreWindowHandle.ToInt64() }
118+
{ "HWND", parentWindow }
116119
});
117120
result &= (FilesystemResult)(status == AppServiceResponseStatus.Success
118121
&& response.Get("Success", false));
@@ -247,7 +250,7 @@ public async Task<IStorageHistory> DeleteItemsAsync(IEnumerable<IStorageItemWith
247250
{ "operationID", operationID },
248251
{ "filepath", string.Join('|', deleleFilePaths) },
249252
{ "permanently", permanently },
250-
{ "HWND", NativeWinApiHelper.CoreWindowHandle.ToInt64() }
253+
{ "HWND", parentWindow }
251254
});
252255
var result = (FilesystemResult)(status == AppServiceResponseStatus.Success
253256
&& response.Get("Success", false));
@@ -265,10 +268,6 @@ public async Task<IStorageHistory> DeleteItemsAsync(IEnumerable<IStorageItemWith
265268
{
266269
progress?.Report(100.0f);
267270
errorCode?.Report(FileSystemStatusCode.Success);
268-
foreach (var item in deleteResult.Items)
269-
{
270-
await associatedInstance.FilesystemViewModel.RemoveFileOrFolderAsync(item.Source);
271-
}
272271
var recycledSources = deleteResult.Items.Where(x => x.Succeeded && x.Destination != null && x.Source != x.Destination)
273272
.Where(x => source.Select(s => s.Path).Contains(x.Source));
274273
if (recycledSources.Any())
@@ -345,7 +344,7 @@ public async Task<IStorageHistory> MoveItemsAsync(IEnumerable<IStorageItemWithPa
345344
{ "filepath", string.Join('|', sourceRename.Select(s => s.Path)) },
346345
{ "destpath", string.Join('|', destinationRename) },
347346
{ "overwrite", false },
348-
{ "HWND", NativeWinApiHelper.CoreWindowHandle.ToInt64() }
347+
{ "HWND", parentWindow }
349348
});
350349
result &= (FilesystemResult)(status == AppServiceResponseStatus.Success
351350
&& response.Get("Success", false));
@@ -362,7 +361,7 @@ public async Task<IStorageHistory> MoveItemsAsync(IEnumerable<IStorageItemWithPa
362361
{ "filepath", string.Join('|', sourceReplace.Select(s => s.Path)) },
363362
{ "destpath", string.Join('|', destinationReplace) },
364363
{ "overwrite", true },
365-
{ "HWND", NativeWinApiHelper.CoreWindowHandle.ToInt64() }
364+
{ "HWND", parentWindow }
366365
});
367366
result &= (FilesystemResult)(status == AppServiceResponseStatus.Success
368367
&& response.Get("Success", false));
@@ -489,7 +488,7 @@ public async Task<IStorageHistory> RestoreItemsFromTrashAsync(IEnumerable<IStora
489488
{ "filepath", string.Join('|', source.Select(s => s.Path)) },
490489
{ "destpath", string.Join('|', destination) },
491490
{ "overwrite", false },
492-
{ "HWND", NativeWinApiHelper.CoreWindowHandle.ToInt64() }
491+
{ "HWND", parentWindow }
493492
});
494493
var result = (FilesystemResult)(status == AppServiceResponseStatus.Success
495494
&& response.Get("Success", false));

src/Files/Filesystem/StorageHistory/StorageHistoryOperations.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ public StorageHistoryOperations(IShellPage associatedInstance, CancellationToken
3232
{
3333
this.associatedInstance = associatedInstance;
3434
this.cancellationToken = cancellationToken;
35-
filesystemOperations = new ShellFilesystemOperations(associatedInstance);
35+
filesystemOperations = new ShellFilesystemOperations(associatedInstance, NativeWinApiHelper.CoreWindowHandle.ToInt64());
3636
filesystemHelpers = this.associatedInstance.FilesystemHelpers;
3737
}
3838

@@ -71,7 +71,7 @@ public async Task<ReturnResult> Redo(IStorageHistory history)
7171
break;
7272
}
7373

74-
await filesystemOperations.CreateShortcutItemsAsync(history.Source, history.Destination.Select(item => item.Path), null, errorCode, cancellationToken);
74+
await Task.Run(() => filesystemOperations.CreateShortcutItemsAsync(history.Source, history.Destination.Select(item => item.Path), null, errorCode, cancellationToken));
7575

7676
break;
7777
}
@@ -133,7 +133,7 @@ await filesystemOperations.RenameAsync(
133133
break;
134134
}
135135

136-
var newHistory = await filesystemOperations.DeleteItemsAsync(history.Source, null, errorCode, false, cancellationToken);
136+
var newHistory = await Task.Run(() => filesystemOperations.DeleteItemsAsync(history.Source, null, errorCode, false, cancellationToken));
137137
if (newHistory != null)
138138
{
139139
// We need to change the recycled item paths (since IDs are different) - for Undo() to work
@@ -288,7 +288,7 @@ await filesystemOperations.RenameAsync(
288288
break;
289289
}
290290

291-
var newHistory = await filesystemOperations.DeleteItemsAsync(history.Destination, null, errorCode, false, cancellationToken);
291+
var newHistory = await Task.Run(() => filesystemOperations.DeleteItemsAsync(history.Destination, null, errorCode, false, cancellationToken));
292292
if (newHistory != null)
293293
{
294294
// We need to change the recycled item paths (since IDs are different) - for Redo() to work

0 commit comments

Comments
 (0)