Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop ReadDirectoryChangesW from other thread

I use next code to know when a files is changed in a certain folder:

HANDLE hDir = ::CreateFile(path, FILE_LIST_DIRECTORY, FILE_SHARE_READ|FILE_SHARE_WRITE|FILE_SHARE_DELETE, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS|FILE_FLAG_OVERLAPPED, NULL);

FILE_NOTIFY_INFORMATION returnData[1024];
DWORD returnDataSize = 0;                   

while(ReadDirectoryChangesW(hDir, returnData, sizeof(returnData), TRUE, FILE_NOTIFY_CHANGE_FILE_NAME|FILE_NOTIFY_CHANGE_DIR_NAME|FILE_NOTIFY_CHANGE_LAST_WRITE, &returnDataSize, NULL, NULL))
{
    ...
}

ReadDirectoryChangesW blocks the thread until a file changes occurs. Is there any way to stop/force return from this function?

like image 680
Mircea Ispas Avatar asked Feb 22 '23 21:02

Mircea Ispas


2 Answers

From your description, it sounds like CancelIoEx should do the trick. Obviously, you need another thread for that, since you're now calling it synchronously. That blocks the calling thread, so you can't do anyting from that thread, not even stop.

like image 152
MSalters Avatar answered Feb 24 '23 22:02

MSalters


I think you need to take a look at this blog post: http://qualapps.blogspot.com/2010/05/understanding-readdirectorychangesw.html

It's a lengthy post, but it's very informative and it talks about all the issues associated with this method.

like image 31
Kiril Avatar answered Feb 24 '23 22:02

Kiril