@@ -878,18 +878,19 @@ await Windows.System.Threading.ThreadPool.RunAsync((x) =>
878878 fixed ( byte * pBuff = buff )
879879 {
880880 ref var notifyInformation = ref Unsafe . As < byte , FILE_NOTIFY_INFORMATION > ( ref buff [ 0 ] ) ;
881- try
881+ if ( hWatchDir . ToInt64 ( ) != - 1 )
882882 {
883883 NativeDirectoryChangesHelper . ReadDirectoryChangesW ( hWatchDir , pBuff ,
884884 4096 , false ,
885885 notifyFilters , null ,
886886 ref overlapped , null ) ;
887887 }
888- catch ( Exception )
888+ else
889889 {
890+ IsWatching = false ;
890891 return ;
891892 }
892-
893+
893894 var rc = WaitForSingleObjectEx ( overlapped . hEvent , INFINITE , true ) ;
894895
895896 const uint FILE_ACTION_ADDED = 0x00000001 ;
@@ -900,6 +901,8 @@ await Windows.System.Threading.ThreadPool.RunAsync((x) =>
900901
901902 uint offset = 0 ;
902903 ref var notifyInfo = ref Unsafe . As < byte , FILE_NOTIFY_INFORMATION > ( ref buff [ offset ] ) ;
904+ if ( hWatchDir . ToInt64 ( ) == - 1 ) { IsWatching = false ; return ; }
905+
903906 do
904907 {
905908 notifyInfo = ref Unsafe . As < byte , FILE_NOTIFY_INFORMATION > ( ref buff [ offset ] ) ;
@@ -913,50 +916,36 @@ await Windows.System.Threading.ThreadPool.RunAsync((x) =>
913916 }
914917
915918 uint action = notifyInfo . Action ;
916- //await CoreApplication.MainView.CoreWindow.Dispatcher.RunAsync(CoreDispatcherPriority.Normal,
917- // () =>
918- // {
919+
919920 switch ( action )
920921 {
921922 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." ) ;
927925 break ;
928926 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." ) ;
934929 break ;
935930 case FILE_ACTION_MODIFIED :
936931 Debug . WriteLine ( "File " + FileName + " had attributes modified in the working directory." ) ;
937932 break ;
938933 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." ) ;
944936 break ;
945937 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." ) ;
951940 break ;
952941 default :
953942 Debug . WriteLine ( "File " + FileName + " performed an action in the working directory." ) ;
954943 break ;
955944 }
956- //});
945+
957946 offset += notifyInfo . NextEntryOffset ;
958947
959- } while ( notifyInfo . NextEntryOffset != 0 ) ;
948+ } while ( notifyInfo . NextEntryOffset != 0 && hWatchDir . ToInt64 ( ) != - 1 ) ;
960949
961950 //ResetEvent(overlapped.hEvent);
962951 Debug . WriteLine ( "\n \n Task running...\n \n " ) ;
0 commit comments