Wrote a function (I don’t think it’s right) that tracks changes in a folder:

bool if_changes(wstring path) { path += L"\\"; LPWSTR lpath = const_cast<LPWSTR>(path.c_str()); HANDLE hDir = CreateFileW(lpath, FILE_LIST_DIRECTORY, FILE_SHARE_READ | FILE_SHARE_DELETE | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS | FILE_FLAG_OVERLAPPED, NULL); BYTE outBuffer[5120]; VOID *pBuf = (BYTE*)&outBuffer; DWORD InfoNotify; BOOL ResultReadChange; DWORD outSize = sizeof(outBuffer); HANDLE ReadChange; ReadChange = FindFirstChangeNotificationW(lpath, TRUE, FILE_NOTIFY_CHANGE_FILE_NAME | FILE_NOTIFY_CHANGE_DIR_NAME | FILE_NOTIFY_CHANGE_SIZE); ResultReadChange = FindNextChangeNotification(ReadChange); FindCloseChangeNotification(ReadChange); return ResultReadChange; } 

In the main program loop, by the condition of changes in the folder, I perform fairly process-intensive actions. But for some reason, the function constantly sees changes in the folder, even when they are not really there. I used ReadDirectoryChangesW() , but it does not continue program execution, but waits for any changes in the folder when I need to check the changes and run further. Actually the question: what did I do wrong and what should I do to work as in my description?

    1 answer 1

    You misuse these APIs. FindNextChangeNotification() returns the success of its execution, but not the presence of changes. An example of using from MSDN: https://msdn.microsoft.com/ru-ru/library/windows/desktop/aa365261(v=vs.85).aspx

    • Well, unless the success of the implementation does not imply that changes have occurred in the folder? - Y. Volegov
    • @ Y.Volegov no. This means that the API function worked correctly. These APIs require the use of Wait-functions (as written in MSDN), and do not work as you expect from them. - Vladimir Martyanov