【问题标题】:What's the best practice to recover from a FileSystemWatcher error?从 FileSystemWatcher 错误中恢复的最佳实践是什么?
【发布时间】:2012-07-26 10:07:32
【问题描述】:

在引发FileSystemWatcher.Error 事件后,我不知道下一步该做什么。 异常可以是 [相对] 次要的异常,例如

在目录中一次更改太多

这不会影响watcher的观看过程,但也可能是个大问题——比如被监控的目录被删除了,这种情况下watcher就不再起作用了。

我的问题是处理错误事件的最佳方法是什么?

【问题讨论】:

  • 停止使用FileSystemWatcher,写轮询服务不可靠。
  • 我正在使用修改后的 FileSystemWatcher,它解决了原始观察程序的大部分可靠性问题[溢出问题、重复事件和网络路径监视]
  • 或者检查在屏幕上绘制和显示之前发生的事件的文件。适用于上下文菜单。 (编辑:听起来不错 Nissim,分享?:P)
  • 我打算分享它,一旦我完成关闭所有边缘。我会及时通知你
  • @saj 什么使用?

标签: c# .net exception-handling filesystemwatcher


【解决方案1】:

肯定取决于错误?

  1. 如果由于缓冲区溢出(许多更改)导致数据过多,请创建一个列表目录并获取您需要的更改。
  2. 如果数据过多是因为您没有足够快地处理 FileSystemWatcher 事件,请确保您有效地处理它。
  3. 已删除目录,除了释放 FileSystemWatcher 或可能再次监视父目录以重新创建该目录名称之外,无法对其执行任何操作。

【讨论】:

    【解决方案2】:

    我会简单地获取内部异常类型,然后根据每个错误决定要做什么(重新启动或失败)。

    所以

    myWatcher.Error += new ErrorEventHandler(OnError);
    

    关注

    private static void OnError(object source, ErrorEventArgs e)
    {
        if (e.GetException().GetType() == typeof(InternalBufferOverflowException))
        {
            //  This can happen if Windows is reporting many file system events quickly 
            //  and internal buffer of the  FileSystemWatcher is not large enough to handle this
            //  rate of events. The InternalBufferOverflowException error informs the application
            //  that some of the file system events are being lost.
            Console.WriteLine(("The file system watcher experienced an internal buffer overflow: " + e.GetException().Message));
        }
    }
    

    【讨论】:

    • 我实际上通过使用阻塞队列解决了所有溢出问题
    猜你喜欢
    • 2012-01-14
    • 2011-09-22
    • 1970-01-01
    • 1970-01-01
    • 2020-06-05
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2015-07-05
    相关资源
    最近更新 更多