当目录中同时添加很多文件时FileSystemWatcher无法正常工作
当许多文件同时添加到目录中时,FileSystemWatcher 无法正常工作...
Watcher 根本找不到目录中的所有文件 - 仅当文件被一一放入文件夹中时 - 如果文件很多则不会文件同时复制到该文件夹...
创建线程是问题的解决方案还是有其他方法来处理问题?
FileSystemWatcher does not work properly when many files are added to the directory at the same time...
The Watcher simply doesn't find all the files in the directory - only if the files are placed in the folder one by one - not if lots of files are copied to the folder at the same time...
Is the creation of Threads the solution to the problem or is there another way to handle the problem ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
该类的文档详细介绍了该问题:
因此,在这种情况下,线程可能不会有太大帮助。您可能想要增加缓冲区大小(但它应该有多大可能很大程度上取决于计算机和磁盘本身的速度)或通过设置适当的过滤器来限制您感兴趣的文件。
The documentation on that class details that problem:
So, threads probably won't help you much in this case. You probably want to either increase the buffer size (but how large it should be may well depend on the speed of the computer and the disk itself) or constrain what files you are interested in by setting the appropriate filter.
C# 对我来说是新的,我在同样的问题上挣扎了近一周。我有这样的情况:
我什至将计时器间隔设置为 1 毫秒,但缺少一些新文件事件。我尝试从
onCreated
事件内部更新notificationsListBox
,但总是收到交叉引用错误。直到我发现观察者onCreated
事件是在主方法线程之外的线程中执行的,所以,简而言之,这就是我的解决方案:我包括
public delegate void Action()
作为我的类的属性,然后使用Invoke
从onCreated
事件内部更新notificationsListBox
。接下来是片段代码:因此不再需要计时器及其代码。
这对我来说非常有效,我希望它对任何有类似情况的人都有效。
此致!!!
C# is new to me and I struggled with the same trouble for nearly a week. I had this:
I even set the timer interval to 1 millisecond and yet some new file events were missing. I tried to update the
notificationsListBox
from inside theonCreated
event but I always got a Cross-reference error. This was until I found out that the watcheronCreated
event is executed in a thread other than the one of the main method thread, so, in a nut shell, this is my solution:I included
public delegate void Action()
as an attribute of my class and then usedInvoke
to update thenotificationsListBox
from inside theonCreated
event. Next, the piece code:So the timer and its code are no longer necessary.
This works excellent for me and I hope it does for anyone with a similar situation.
Best regards!!!
尝试这样的事情。
try something like this.