On a large folder (~300,000 objects) FindNextFile can take up to 20 secs to respond with just a single file. I assume there's some bulk operation going on in the background but it makes the operation very difficult to cancel.
Is there a way to run FindNextFile in an asynchronous mode so that it can be cancelled if the information is no longer required?
Win7, x64, NTFS.
Side note: Once the information has been cached by Windows FindNextFile doesn't appear to have this issue. It's only on the first time it tries to enumerate files in a large folder.
No, FindNextFile is synchronous and there's no asynchronous function that's comparable. The usual solution to this sort of problem is multithreading. You need two threads: a UI thread that should always remain responsive to the user and a worker thread that does the FindNextFile calls. I would use a queue with a locking mechanism. The logic would look something like this:
Worker Thread:
FindFirstFile();
do
{
LockQueue();
AddFileToQueue();
UnlockQueue();
while (FindNextFile() && !UserCanceled());
SetAllFilesDone();
UI Thread:
while (!UserCanceled() && !AllFilesDone())
{
LockQueue();
GetFileFromQueue();
UnlockQueue();
ProcessFile();
}