From: Tina on
I noticed that when I call the FindFirstFile or FindNextFile on a
large folder of tens of thousands of files, it can take several
minutes at Windows start up time before the function even returns. My
question is whether there exists an API I can use to cancel the find?
I'd like to use this API to possibly cancel the search immediately (or
close to immediately) from a different thread that will check on
whether or not the Find has been completed.
From: Tina on
On Nov 11, 12:02 pm, Tina <mstin...(a)gmail.com> wrote:
> I noticed that when I call the FindFirstFile or FindNextFile on a
> large folder of tens of thousands of files, it can take several
> minutes at Windows start up time before the function even returns. My
> question is whether there exists an API I can use to cancel the find?
> I'd like to use this API to possibly cancel the search immediately (or
> close to immediately) from a different thread that will check on
> whether or not the Find has been completed.

Also, this is on the Windows XP platform so I can't use CancelIOEx. In
any case, I don't think CancelIOEx is useable with search handles
(which is what is returned by FindFirstFile and FindNextFile), only
file handles. Correct me if I'm wrong.
From: Seetharam on
Can't you have some flag to check in the loop to exit?

Modifying the sample in http://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspx,

....
hFind = FindFirstFile(szDir, &ffd);
if (INVALID_HANDLE_VALUE == hFind)
return dwError;

// List all the files in the directory with some info about them.
do
{
if(stopLooping) // this will be a volatile BOOL flag set to
TRUE from another thread when you want to stop.
break;

if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
_tprintf(TEXT(" %s <DIR>\n"), ffd.cFileName);
}
else
{
filesize.LowPart = ffd.nFileSizeLow;
filesize.HighPart = ffd.nFileSizeHigh;
_tprintf(TEXT(" %s %ld bytes\n"), ffd.cFileName,
filesize.QuadPart);
}
}
while (FindNextFile(hFind, &ffd) != 0);
.....

Or look up how to use events in a worker thread. Here's a good
example:
http://www.ucancode.net/Visual_C_MFC_Example/WaitForSingleObject-_beginthreadex-SetEvent-Start-Stop-Thread-VC-MFC-Example.htm


-Seetharam
From: Tina on
That's not actually the problem. The problem is with the
FindFirstFile, FindNextFile functions themselves. You can "break out"
of the function once it's been called. It's not the loop that is
taking a long time, it is returning from the FindFirstFile/
FindNextFile calls that takes a long time.

On Nov 12, 1:32 pm, Seetharam <smi...(a)gmail.com> wrote:
> Can't you have some flag to check in the loop to exit?
>
> Modifying the sample inhttp://msdn.microsoft.com/en-us/library/aa365200%28VS.85%29.aspx,
>
> ...
>     hFind = FindFirstFile(szDir, &ffd);
>    if (INVALID_HANDLE_VALUE == hFind)
>       return dwError;
>
>    // List all the files in the directory with some info about them.
>    do
>    {
>        if(stopLooping) // this will be a volatile BOOL flag set to
> TRUE from another thread when you want to stop.
>              break;
>
>       if (ffd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
>       {
>          _tprintf(TEXT("  %s   <DIR>\n"), ffd.cFileName);
>       }
>       else
>       {
>          filesize.LowPart = ffd.nFileSizeLow;
>          filesize.HighPart = ffd.nFileSizeHigh;
>          _tprintf(TEXT("  %s   %ld bytes\n"), ffd.cFileName,
> filesize.QuadPart);
>       }
>    }
>    while (FindNextFile(hFind, &ffd) != 0);
> ....
>
> Or look up how to use events in a worker thread. Here's a good
> example:http://www.ucancode.net/Visual_C_MFC_Example/WaitForSingleObject-_beg...
>
> -Seetharam

From: Lamblion on
> On Nov 12, 6:17 pm, Tina <mstin...(a)gmail.com> wrote:
> That's not actually the problem. The problem is with the
> FindFirstFile, FindNextFile functions themselves. You can "break out"
> of the function once it's been called. It's not the loop that is
> taking a long time, it is returning from the FindFirstFile/
> FindNextFile calls that takes a long time.

Set a timer and put FindFirstFile and the timer in a while loop, i.e.,
while( seconds < 1000) or whatever.