FindFirstChangeNotification 两次通知更改 [英] FindFirstChangeNotification is notifying about changes twice
问题描述
我想监视文件系统中的文件夹.假设我想监视文件夹:C:\MyNewFolder
I want to monitor a folder in my file system. Let say I want to monitor the folder: C:\MyNewFolder
我有这个代码来做:
HANDLE ChangeHandle=FindFirstChangeNotification(_T("C:\\\MyNewFolder"),FALSE,FILE_NOTIFY_CHANGE_LAST_WRITE);
for(;;)
{
DWORD Wait=WaitForSingleObject(ChangeHandle,INFINITE);
if (Wait == WAIT_OBJECT_0)
{
MessageBox(NULL,_T("Change"),_T("Change"),MB_OK);
FindNextChangeNotification(ChangeHandle);
}
else
{
break;
}
}
我想要一个消息框来通知我文件夹中的任何文件更改.该代码工作正常,但我有一个问题.问题是我每次更改都会收到 2 条通知.我的代码有什么问题?谢谢.
I want to have a messagebox that notifying me about any file change in my folder. That code works fine but I have one problem. The problem is that I got 2 notification for each change. What is the problem with my code? Thanks.
推荐答案
这完全正常.对文件的更改通常涉及对文件数据的更改以及对目录条目的更改.文件长度和最后写入日期等元数据属性存储在那里.因此,您会收到两者的通知.ReadDirectoryChangesW() 不区分两者.
This is entirely normal. A change to a file usually involves a change to the file data as well as a change to the directory entry. Metadata properties like the file length and the last write date are stored there. So you'll get a notification for both. ReadDirectoryChangesW() doesn't otherwise distinguish between the two.
这与对同一文件进行多次更改的过程没有什么不同.确保能够处理这两种情况.这通常涉及一个计时器,因此您不会在通知上执行的操作数量过多.通常还需要这样的计时器,因为正在更改文件的进程仍然对其进行了锁定,以防止您对文件进行任何操作.在进程关闭文件之前,不确定的时间之后.
This is not different from a process making multiple changes to the same file. Be sure to be able to handle both conditions. This usually involves a timer so you don't go overboard with the number of operations you perform on a notification. Such a timer is also often required because the process that is changing the file still has a lock on it that prevents you from doing anything with the file. Until the process closes the file, an indeterminate amount of time later.
这篇关于FindFirstChangeNotification 两次通知更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!