1
1
using Files . DataModels ;
2
2
using Files . Dialogs ;
3
3
using Files . Enums ;
4
+ using Files . Extensions ;
4
5
using Files . Helpers ;
5
6
using Microsoft . Toolkit . Mvvm . ComponentModel ;
6
7
using Microsoft . Toolkit . Mvvm . Input ;
9
10
using System . Collections . Generic ;
10
11
using System . Collections . ObjectModel ;
11
12
using System . Linq ;
13
+ using System . Threading ;
12
14
using System . Threading . Tasks ;
13
15
using System . Windows . Input ;
14
16
using Windows . UI . Xaml . Controls ;
@@ -106,16 +108,28 @@ public bool MustResolveConflicts
106
108
107
109
public ICommand ApplyToAllCommand { get ; private set ; }
108
110
111
+ public ICommand ClosingCommand { get ; private set ; }
112
+
113
+ private CancellationTokenSource ClosingCts { get ; }
114
+
109
115
public FilesystemOperationDialogViewModel ( )
110
116
{
117
+ ClosingCts = new CancellationTokenSource ( ) ;
118
+
111
119
// Create commands
112
120
PrimaryButtonCommand = new RelayCommand ( PrimaryButton ) ;
113
121
SecondaryButtonCommand = new RelayCommand ( SecondaryButton ) ;
122
+
114
123
LoadedCommand = new RelayCommand ( ( ) =>
115
124
{
116
125
UpdatePrimaryButtonEnabled ( ) ;
117
126
} ) ;
118
127
128
+ ClosingCommand = new RelayCommand ( ( ) =>
129
+ {
130
+ ClosingCts . Cancel ( ) ;
131
+ } ) ;
132
+
119
133
ApplyToAllCommand = new RelayCommand < string > ( s =>
120
134
{
121
135
ApplyConflictOptionToAll ( ( FileNameConflictResolveOptionType ) int . Parse ( s ) ) ;
@@ -284,29 +298,30 @@ public static FilesystemOperationDialog GetDialog(FilesystemItemsOperationDataMo
284
298
} ;
285
299
viewModel . Items = new ObservableCollection < FilesystemOperationItemViewModel > ( itemsData . ToItems (
286
300
viewModel . UpdatePrimaryButtonEnabled , viewModel . OptionGenerateNewName , viewModel . OptionReplaceExisting , viewModel . OptionSkip ) ) ;
287
- _ = LoadItemsIcon ( viewModel . Items ) ;
301
+ _ = LoadItemsIcon ( viewModel . Items , viewModel . ClosingCts . Token ) ;
288
302
FilesystemOperationDialog dialog = new FilesystemOperationDialog ( viewModel ) ;
289
303
290
304
return dialog ;
291
305
}
292
306
293
- private static async Task LoadItemsIcon ( IEnumerable < FilesystemOperationItemViewModel > items )
307
+ private static async Task LoadItemsIcon ( IEnumerable < FilesystemOperationItemViewModel > items , CancellationToken token )
294
308
{
295
- await Task . Run ( ( ) => Task . WhenAll ( items . ToList ( ) . Select ( async ( item ) =>
309
+ await items . ParallelForEach ( async ( item ) =>
296
310
{
297
311
try
298
312
{
299
313
var iconData = await FileThumbnailHelper . LoadIconFromPathAsync ( item . SourcePath , 64u , Windows . Storage . FileProperties . ThumbnailMode . ListView ) ;
300
314
if ( iconData != null )
301
315
{
316
+ if ( token . IsCancellationRequested ) return ;
302
317
await Windows . ApplicationModel . Core . CoreApplication . MainView . Dispatcher . RunAsync ( Windows . UI . Core . CoreDispatcherPriority . Low , async ( ) =>
303
318
{
304
319
item . ItemIcon = await iconData . ToBitmapAsync ( ) ;
305
320
} ) ;
306
321
}
307
322
}
308
323
catch { }
309
- } ) ) ) ;
324
+ } , 10 , token ) ;
310
325
}
311
326
}
312
327
}
0 commit comments