FileSystemWatcher除了包含320K子目录的根文件夹外,甚至无法运行一次 [英] FileSystemWatcher not working even once except for root folder having 320K subdirectories

查看:52
本文介绍了FileSystemWatcher除了包含320K子目录的根文件夹外,甚至无法运行一次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下代码监视包含子目录的目录中发生的更改.当前,我正在运行此应用程序的三个不同副本,其中两个运行正常.无法使用的应用程序中包含超过32万个子目录.

I am using below code to monitor changes happening in a directory including subdirectories. Currently, I am running three different copies of this application and two of them are working fine. Application for which it is not working contains more than 320K subdirectories in it.

我尝试增加 InternalBufferSize ,但是什么也没有发生.它在根文件夹下工作,而在任何子目录下都不工作.

I tried to increase InternalBufferSize but nothing happens. It is working at the root folder and not working for any of the subdirectories.

此外,同一应用程序的其他2个副本(包括子目录)也可以正常工作,这些副本可以监视不同地理位置上的不同网络路径.

Also, 2 other copies of the same application monitoring different network paths on different geographical locations are working fine including subdirectories as well.

FileSystemWatcher watcher = new FileSystemWatcher
{
    Path = path,
    IncludeSubdirectories = true,
    Filter = "*.txt",
    NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite |
        NotifyFilters.FileName | NotifyFilters.DirectoryName
};
watcher.Created += Watcher_Created;
watcher.Changed += Watcher_Changed;
watcher.Deleted += Watcher_Deleted;
watcher.Renamed += Watcher_Renamed;
watcher.Error += Watcher_Error;
watcher.EnableRaisingEvents = true;

PS:轮询不是一种选择.扫描整个320K目录需要3天.根目录文件夹包含2000多个子目录,每个子目录中最多包含8个子文件夹级别.

PS: Polling is not an option. It takes 3 days to scan entirely 320K directories. The root-level folder has 2000 plus subdirectories and each subdirectory has up-to 8 levels of subfolders in it

我已经与INFRA团队进行了核对,并且知道共享文件夹在EMC Isilon NAS存储上.

https://social.msdn.microsoft.com/Forums/vstudio/en-US/38adb37d-bbfc-40d6-8b32-a5c1c7d8d4a3/文件系统监视程序监视nas服务器上的unc路径是否存在任何限制?forum = netfxbcl

推荐答案

与其同步执行事件处理程序,不如将其卸载到 ThreadPool .这样可以最大程度地减少 FileSystemWatcher 的内部缓冲区溢出的风险.下面的 Offload 方法可用于此目的:

Instead of executing the event handlers synchronously, it may help to offload them to the ThreadPool. This will minimize the risk of overflowing the internal buffer of the FileSystemWatcher. The Offload method below could be used for this purpose:

public static FileSystemEventHandler Offload(FileSystemEventHandler handler)
{
    return (s, e) => ThreadPool.QueueUserWorkItem(_ => handler(s, e));
}

用法示例:

var fsw = new FileSystemWatcher(path);
fsw.Created += Offload((s, e) =>
{
    Console.WriteLine($"Created: {e.Name}");
});

这并不是一种有效的解决方案,而只是解决缓冲区溢出问题的一种简单方法(假设这是您正在观察的问题的原因).

This is not intended as an efficient solution, but just as an easy fix to the buffer-overflow problem (assuming that it's the cause for the issues you are observing).

这篇关于FileSystemWatcher除了包含320K子目录的根文件夹外,甚至无法运行一次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆