@@ -679,8 +679,10 @@ public async void RapidAddItemsToCollectionAsync(string path)
679679 }
680680 else
681681 {
682- await EnumerateItemsFromStandardFolder ( path ) ;
683- WatchForDirectoryChanges ( path ) ;
682+ if ( await EnumerateItemsFromStandardFolder ( path ) )
683+ {
684+ WatchForDirectoryChanges ( path ) ;
685+ }
684686 }
685687
686688 if ( FilesAndFolders . Count == 0 )
@@ -783,7 +785,7 @@ public async Task EnumerateItemsFromSpecialFolder(string path)
783785 }
784786 }
785787
786- public async Task EnumerateItemsFromStandardFolder ( string path )
788+ public async Task < bool > EnumerateItemsFromStandardFolder ( string path )
787789 {
788790 // Flag to use FindFirstFileExFromApp or StorageFolder enumeration
789791 bool enumFromStorageFolder = false ;
@@ -796,15 +798,15 @@ public async Task EnumerateItemsFromStandardFolder(string path)
796798 {
797799 var consentDialogDisplay = new ConsentDialog ( ) ;
798800 await consentDialogDisplay . ShowAsync ( ContentDialogPlacement . Popup ) ;
799- return ;
801+ return false ;
800802 }
801803 catch ( FileNotFoundException )
802804 {
803805 await DialogDisplayHelper . ShowDialog (
804806 "FolderNotFoundDialog/Title" . GetLocalized ( ) ,
805807 "FolderNotFoundDialog/Text" . GetLocalized ( ) ) ;
806808 IsLoadingItems = false ;
807- return ;
809+ return false ;
808810 }
809811 catch ( Exception e )
810812 {
@@ -817,7 +819,7 @@ await DialogDisplayHelper.ShowDialog(
817819 {
818820 await DialogDisplayHelper . ShowDialog ( "DriveUnpluggedDialog/Title" . GetLocalized ( ) , e . Message ) ;
819821 IsLoadingItems = false ;
820- return ;
822+ return false ;
821823 }
822824 }
823825
@@ -873,13 +875,18 @@ await DialogDisplayHelper.ShowDialog(
873875 FileSizeBytes = 0
874876 } ;
875877 await EnumFromStorageFolder ( ) ;
878+ return true ;
876879 }
877880 else
878881 {
879- FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS . FindExInfoBasic ;
880- int additionalFlags = FIND_FIRST_EX_LARGE_FETCH ;
881- IntPtr hFile = FindFirstFileExFromApp ( path + "\\ *.*" , findInfoLevel , out WIN32_FIND_DATA findData , FINDEX_SEARCH_OPS . FindExSearchNameMatch , IntPtr . Zero ,
882- additionalFlags ) ;
882+ ( IntPtr hFile , WIN32_FIND_DATA findData ) = await Task . Run ( ( ) =>
883+ {
884+ FINDEX_INFO_LEVELS findInfoLevel = FINDEX_INFO_LEVELS . FindExInfoBasic ;
885+ int additionalFlags = FIND_FIRST_EX_LARGE_FETCH ;
886+ IntPtr hFileTsk = FindFirstFileExFromApp ( path + "\\ *.*" , findInfoLevel , out WIN32_FIND_DATA findDataTsk , FINDEX_SEARCH_OPS . FindExSearchNameMatch , IntPtr . Zero ,
887+ additionalFlags ) ;
888+ return ( hFileTsk , findDataTsk ) ;
889+ } ) . WithTimeout ( TimeSpan . FromSeconds ( 5 ) ) ;
883890
884891 DateTime itemDate = DateTime . UtcNow ;
885892 try
@@ -909,9 +916,16 @@ await DialogDisplayHelper.ShowDialog(
909916 } ;
910917
911918 var count = 0 ;
912- if ( hFile . ToInt64 ( ) == - 1 )
919+ if ( hFile == IntPtr . Zero )
920+ {
921+ await DialogDisplayHelper . ShowDialog ( "DriveUnpluggedDialog/Title" . GetLocalized ( ) , "" ) ;
922+ IsLoadingItems = false ;
923+ return false ;
924+ }
925+ else if ( hFile . ToInt64 ( ) == - 1 )
913926 {
914927 await EnumFromStorageFolder ( ) ;
928+ return false ;
915929 }
916930 else
917931 {
@@ -945,6 +959,7 @@ await DialogDisplayHelper.ShowDialog(
945959 } while ( FindNextFile ( hFile , out findData ) ) ;
946960
947961 FindClose ( hFile ) ;
962+ return true ;
948963 }
949964 }
950965 }
0 commit comments