FileSystemWatcher fails with 1000 files creations

半世苍凉 提交于 2019-12-10 13:50:05

问题


I'm using FileSystemWatcher to monitor folder A. if a file is created or copied into that folder, it will be moved to folder B.

I created a file in folder A by right clicking, selecting "New", select "Text Document". The file is moved to folder B immediately. But if I use a program to create 1000 files into folder A then they are not moved to folder B. As it would seem FileSystemWatcher can't detect 1000 files were created by program.

Can anyone explain this for me?


回答1:


Looks like a 1000 files will overrun the FileSystemWatcher's buffer. Great explanation here.

Link Excerpt:

Buffer Size and the Error Event

The FileSystemWatcher class works by capturing all of the relevant file and older changes and placing them into a buffer. This is then processed one change at a time until all of the notifications have been dealt with and the buffer is empty. By default, the internal buffer has a size of eight kilobytes (8192 bytes). Each event can take up to sixteen bytes of the buffer for its data, not including the file name. This means that when there are a lot of changes in a short period of time, the buffer can quickly become overloaded and notifications can be lost.

Handling the proper error event from FileSystemWatcher will allow you to code for this possibility. E.g. Examine and/or operate on the error event's folder with your own logic...



来源:https://stackoverflow.com/questions/4446744/filesystemwatcher-fails-with-1000-files-creations

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!