• 使用延迟的FileSystemWatcher来避免重复触发事件


    问题:

      程序里需要监视某个目录下的文件变化情况: 一旦目录中出现新文件或者旧的文件被覆盖,程序需要读取文件内容并进行处理;但在实际处理中发现当一个文件产生变化时,Change事件被反复触发了好几次。这样可能的结果是造成同一文件的重复处理。
    解决方法:

      针对上面的问题,于是写了一个可以延迟FileSystemWatcher发出的事件的Class DelayFileSystemWatcher。

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Threading;

    namespace Utility
    {
    public class DelayFileSystemWatcher
    {
    private readonly Timer m_Timer;
    private readonly Int32 m_TimerInterval;
    private readonly FileSystemWatcher m_FileSystemWatcher;
    private readonly FileSystemEventHandler m_FileSystemEventHandler;
    private readonly Dictionary<String, FileSystemEventArgs> m_ChangedFiles = new Dictionary<string, FileSystemEventArgs>();

    public DelayFileSystemWatcher(string path, string filter, FileSystemEventHandler watchHandler, int timerInterval)
    {
    m_Timer = new Timer(OnTimer, null, Timeout.Infinite, Timeout.Infinite);
    m_FileSystemWatcher = new FileSystemWatcher(path, filter);
    m_FileSystemWatcher.NotifyFilter = NotifyFilters.FileName | NotifyFilters.LastWrite | NotifyFilters.CreationTime;
    m_FileSystemWatcher.Created += fileSystemWatcher_Changed;
    m_FileSystemWatcher.Changed += fileSystemWatcher_Changed;
    m_FileSystemWatcher.Deleted += fileSystemWatcher_Changed;
    m_FileSystemWatcher.Renamed += fileSystemWatcher_Changed;
    m_FileSystemWatcher.EnableRaisingEvents = true;
    m_FileSystemEventHandler = watchHandler;
    m_TimerInterval = timerInterval;
    }

    public void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
    {
    lock (m_ChangedFiles)
    {
    if (!m_ChangedFiles.ContainsKey(e.Name))
    {
    m_ChangedFiles.Add(e.Name, e);
    }
    }
    m_Timer.Change(m_TimerInterval, Timeout.Infinite);
    }

    private void OnTimer(object state)
    {
    Dictionary<String, FileSystemEventArgs> tempChangedFiles = new Dictionary<String, FileSystemEventArgs>();

    lock (m_ChangedFiles)
    {
    foreach (KeyValuePair<string, FileSystemEventArgs> changedFile in m_ChangedFiles)
    {
    tempChangedFiles.Add(changedFile.Key, changedFile.Value);
    }
    m_ChangedFiles.Clear();
    }

    foreach (KeyValuePair<string, FileSystemEventArgs> changedFile in tempChangedFiles)
    {
    m_FileSystemEventHandler(this, changedFile.Value);
    }
    }
    }
    }

    使用方式如下:

            s_DelayFileSystemWatcher = new DelayFileSystemWatcher(@"C:\Temp", "*.xml", fileSystemWatcher_Changed, 1500);

    private static void fileSystemWatcher_Changed(object sender, FileSystemEventArgs e)
    {
    switch (e.ChangeType)
    {
    case WatcherChangeTypes.Created:
    //TODO
    break;
    case WatcherChangeTypes.Deleted:
    //TODO
    break;
    case WatcherChangeTypes.Changed:
    //TODO
    break;
    default:
    break;
    }
    }
  • 相关阅读:
    oracle去除字符串中间的空格
    java代理模式
    js方法中的this
    js中访问对象的方法
    Hadoop学习笔记
    查看电脑硬件常用命令
    Ubuntu18.0.4配置Hadoop1.2.1环境
    Entwurfsmuster
    WEB Front-end Development Technology
    Objekt Orientierte Programmierung C++
  • 原文地址:https://www.cnblogs.com/zanxiaofeng/p/1930583.html
Copyright © 2020-2023  润新知