@@ -878,18 +878,19 @@ await Windows.System.Threading.ThreadPool.RunAsync((x) =>
878
878
fixed ( byte * pBuff = buff )
879
879
{
880
880
ref var notifyInformation = ref Unsafe . As < byte , FILE_NOTIFY_INFORMATION > ( ref buff [ 0 ] ) ;
881
- try
881
+ if ( hWatchDir . ToInt64 ( ) != - 1 )
882
882
{
883
883
NativeDirectoryChangesHelper . ReadDirectoryChangesW ( hWatchDir , pBuff ,
884
884
4096 , false ,
885
885
notifyFilters , null ,
886
886
ref overlapped , null ) ;
887
887
}
888
- catch ( Exception )
888
+ else
889
889
{
890
+ IsWatching = false ;
890
891
return ;
891
892
}
892
-
893
+
893
894
var rc = WaitForSingleObjectEx ( overlapped . hEvent , INFINITE , true ) ;
894
895
895
896
const uint FILE_ACTION_ADDED = 0x00000001 ;
@@ -900,6 +901,8 @@ await Windows.System.Threading.ThreadPool.RunAsync((x) =>
900
901
901
902
uint offset = 0 ;
902
903
ref var notifyInfo = ref Unsafe . As < byte , FILE_NOTIFY_INFORMATION > ( ref buff [ offset ] ) ;
904
+ if ( hWatchDir . ToInt64 ( ) == - 1 ) { IsWatching = false ; return ; }
905
+
903
906
do
904
907
{
905
908
notifyInfo = ref Unsafe . As < byte , FILE_NOTIFY_INFORMATION > ( ref buff [ offset ] ) ;
@@ -913,50 +916,36 @@ await Windows.System.Threading.ThreadPool.RunAsync((x) =>
913
916
}
914
917
915
918
uint action = notifyInfo . Action ;
916
- //await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
917
- // () =>
918
- // {
919
+
919
920
switch ( action )
920
921
{
921
922
case FILE_ACTION_ADDED :
922
- if ( ! FilesAndFolders . Select ( x => x . ItemPath ) . Contains ( FileName ) )
923
- {
924
- AddFileOrFolder ( FileName ) ;
925
- Debug . WriteLine ( "File " + FileName + " added to working directory." ) ;
926
- }
923
+ AddFileOrFolder ( FileName ) ;
924
+ Debug . WriteLine ( "File " + FileName + " added to working directory." ) ;
927
925
break ;
928
926
case FILE_ACTION_REMOVED :
929
- if ( FilesAndFolders . Select ( x => x . ItemPath ) . Contains ( FileName ) )
930
- {
931
- RemoveFileOrFolder ( FilesAndFolders . First ( x => x . ItemPath . Equals ( FileName ) ) ) ;
932
- Debug . WriteLine ( "File " + FileName + " removed from working directory." ) ;
933
- }
927
+ RemoveFileOrFolder ( FilesAndFolders . ToList ( ) . First ( x => x . ItemPath . Equals ( FileName ) ) ) ;
928
+ Debug . WriteLine ( "File " + FileName + " removed from working directory." ) ;
934
929
break ;
935
930
case FILE_ACTION_MODIFIED :
936
931
Debug . WriteLine ( "File " + FileName + " had attributes modified in the working directory." ) ;
937
932
break ;
938
933
case FILE_ACTION_RENAMED_OLD_NAME :
939
- if ( FilesAndFolders . Select ( x => x . ItemPath ) . Contains ( FileName ) )
940
- {
941
- RemoveFileOrFolder ( FilesAndFolders . First ( x => x . ItemPath . Equals ( FileName ) ) ) ;
942
- Debug . WriteLine ( "File " + FileName + " will be renamed in the working directory." ) ;
943
- }
934
+ RemoveFileOrFolder ( FilesAndFolders . ToList ( ) . First ( x => x . ItemPath . Equals ( FileName ) ) ) ;
935
+ Debug . WriteLine ( "File " + FileName + " will be renamed in the working directory." ) ;
944
936
break ;
945
937
case FILE_ACTION_RENAMED_NEW_NAME :
946
- if ( ! FilesAndFolders . Select ( x => x . ItemPath ) . Contains ( FileName ) )
947
- {
948
- AddFileOrFolder ( FileName ) ;
949
- Debug . WriteLine ( "File " + FileName + " was renamed in the working directory." ) ;
950
- }
938
+ AddFileOrFolder ( FileName ) ;
939
+ Debug . WriteLine ( "File " + FileName + " was renamed in the working directory." ) ;
951
940
break ;
952
941
default :
953
942
Debug . WriteLine ( "File " + FileName + " performed an action in the working directory." ) ;
954
943
break ;
955
944
}
956
- //});
945
+
957
946
offset += notifyInfo . NextEntryOffset ;
958
947
959
- } while ( notifyInfo . NextEntryOffset != 0 ) ;
948
+ } while ( notifyInfo . NextEntryOffset != 0 && hWatchDir . ToInt64 ( ) != - 1 ) ;
960
949
961
950
//ResetEvent(overlapped.hEvent);
962
951
Debug . WriteLine ( "\n \n Task running...\n \n " ) ;
0 commit comments