2
2
using Files . Common ;
3
3
using Files . Enums ;
4
4
using Files . Helpers ;
5
- using Files . Interacts ;
6
5
using Files . Views . Pages ;
7
6
using Microsoft . Toolkit . Uwp . UI ;
8
7
using System ;
20
19
using Windows . Foundation . Collections ;
21
20
using Windows . Storage ;
22
21
using Windows . Storage . FileProperties ;
23
- using Windows . Storage . Search ;
24
22
using Windows . UI . Core ;
25
23
using Windows . UI . Text ;
26
24
using Windows . UI . Xaml ;
@@ -367,15 +365,8 @@ private void WorkingDirectoryChanged()
367
365
368
366
public void CancelLoadAndClearFiles ( )
369
367
{
370
- if ( aWatcherAction != null )
371
- {
372
- aWatcherAction ? . Cancel ( ) ;
373
-
374
- if ( aWatcherAction . Status != AsyncStatus . Started )
375
- {
376
- CloseWatcher ( ) ;
377
- }
378
- }
368
+ Debug . WriteLine ( "CancelLoadAndClearFiles" ) ;
369
+ CloseWatcher ( ) ;
379
370
380
371
App . CurrentInstance . NavigationToolbar . CanRefresh = true ;
381
372
if ( IsLoadingItems == false ) { return ; }
@@ -669,8 +660,19 @@ public async void RapidAddItemsToCollectionAsync(string path)
669
660
670
661
public void CloseWatcher ( )
671
662
{
672
- CancelIo ( hWatchDir ) ;
673
- //CloseHandle(hWatchDir);
663
+ if ( aWatcherAction != null )
664
+ {
665
+ aWatcherAction ? . Cancel ( ) ;
666
+
667
+ if ( aWatcherAction . Status != AsyncStatus . Started )
668
+ {
669
+ aWatcherAction = null ; // Prevent duplicate execution of this block
670
+ Debug . WriteLine ( "watcher canceled" ) ;
671
+ CancelIoEx ( hWatchDir , IntPtr . Zero ) ;
672
+ Debug . WriteLine ( "watcher handle closed" ) ;
673
+ CloseHandle ( hWatchDir ) ;
674
+ }
675
+ }
674
676
}
675
677
676
678
public async Task EnumerateItemsFromSpecialFolder ( string path )
@@ -857,15 +859,15 @@ public async Task EnumerateItemsFromStandardFolder(string path)
857
859
858
860
private void WatchForDirectoryChanges ( string path )
859
861
{
860
-
862
+ Debug . WriteLine ( "WatchForDirectoryChanges: {0}" , path ) ;
861
863
hWatchDir = CreateFileFromApp ( path , 1 , 1 | 2 | 4 ,
862
864
IntPtr . Zero , 3 , ( uint ) File_Attributes . BackupSemantics | ( uint ) File_Attributes . Overlapped , IntPtr . Zero ) ;
863
865
864
866
byte [ ] buff = new byte [ 4096 ] ;
865
867
866
868
aWatcherAction = Windows . System . Threading . ThreadPool . RunAsync ( ( x ) =>
867
869
{
868
-
870
+ var rand = Guid . NewGuid ( ) ;
869
871
buff = new byte [ 4096 ] ;
870
872
int notifyFilters = FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_FILE_NAME ;
871
873
@@ -880,20 +882,22 @@ private void WatchForDirectoryChanges(string path)
880
882
fixed ( byte * pBuff = buff )
881
883
{
882
884
ref var notifyInformation = ref Unsafe . As < byte , FILE_NOTIFY_INFORMATION > ( ref buff [ 0 ] ) ;
883
- if ( x . Status != AsyncStatus . Canceled )
884
- {
885
- NativeDirectoryChangesHelper . ReadDirectoryChangesW ( hWatchDir , pBuff ,
886
- 4096 , false ,
887
- notifyFilters , null ,
888
- ref overlapped , null ) ;
885
+ if ( x . Status != AsyncStatus . Canceled )
886
+ {
887
+ NativeDirectoryChangesHelper . ReadDirectoryChangesW ( hWatchDir , pBuff ,
888
+ 4096 , false ,
889
+ notifyFilters , null ,
890
+ ref overlapped , null ) ;
889
891
}
890
892
else
891
893
{
892
- return ;
894
+ break ;
893
895
}
894
896
895
- if ( x . Status == AsyncStatus . Canceled ) { return ; }
897
+ Debug . WriteLine ( "waiting: {0}" , rand ) ;
898
+ if ( x . Status == AsyncStatus . Canceled ) { break ; }
896
899
var rc = WaitForSingleObjectEx ( overlapped . hEvent , INFINITE , true ) ;
900
+ Debug . WriteLine ( "wait done: {0}" , rand ) ;
897
901
898
902
const uint FILE_ACTION_ADDED = 0x00000001 ;
899
903
const uint FILE_ACTION_REMOVED = 0x00000002 ;
@@ -903,7 +907,7 @@ private void WatchForDirectoryChanges(string path)
903
907
904
908
uint offset = 0 ;
905
909
ref var notifyInfo = ref Unsafe . As < byte , FILE_NOTIFY_INFORMATION > ( ref buff [ offset ] ) ;
906
- if ( x . Status == AsyncStatus . Canceled ) { return ; }
910
+ if ( x . Status == AsyncStatus . Canceled ) { break ; }
907
911
908
912
do
909
913
{
@@ -919,6 +923,7 @@ private void WatchForDirectoryChanges(string path)
919
923
920
924
uint action = notifyInfo . Action ;
921
925
926
+ Debug . WriteLine ( "action: {0}" , action ) ;
922
927
switch ( action )
923
928
{
924
929
case FILE_ACTION_ADDED :
@@ -949,14 +954,16 @@ private void WatchForDirectoryChanges(string path)
949
954
950
955
} while ( notifyInfo . NextEntryOffset != 0 && x . Status != AsyncStatus . Canceled ) ;
951
956
952
- //ResetEvent(overlapped.hEvent);
953
- Debug . WriteLine ( "\n \n Task running...\n \n " ) ;
957
+ //ResetEvent(overlapped.hEvent);
958
+ Debug . WriteLine ( "Task running..." ) ;
954
959
}
955
960
}
956
961
}
962
+ CloseHandle ( overlapped . hEvent ) ;
963
+ Debug . WriteLine ( "aWatcherAction done: {0}" , rand ) ;
957
964
} ) ;
958
965
959
- Debug . WriteLine ( "\n \n Task exiting...\n \n " ) ;
966
+ Debug . WriteLine ( "Task exiting..." ) ;
960
967
}
961
968
962
969
public void AddFileOrFolder ( ListedItem item )
@@ -1154,7 +1161,7 @@ private async Task AddFolder(StorageFolder folder)
1154
1161
IsLoadingItems = false ;
1155
1162
return ;
1156
1163
}
1157
-
1164
+
1158
1165
_filesAndFolders . Add ( new ListedItem ( folder . FolderRelativeId )
1159
1166
{
1160
1167
//FolderTooltipText = tooltipString,
@@ -1278,6 +1285,7 @@ public void Dispose()
1278
1285
{
1279
1286
_addFilesCTS ? . Dispose ( ) ;
1280
1287
_semaphoreCTS ? . Dispose ( ) ;
1288
+ CloseWatcher ( ) ;
1281
1289
}
1282
1290
}
1283
- }
1291
+ }
0 commit comments