【问题标题】:Use timer with fileSystemWatcher in C#在 C# 中使用带有 fileSystemWatcher 的计时器
【发布时间】:2019-04-04 16:36:42
【问题描述】:

场景是我有一个根文件夹来监视任何新文件夹(包含文件)并设置一个计时器来单独压缩每个文件夹。但是,我无法判断文件夹中的文件是否是调用 zip 函数之前的最后一个文件,因此,只要在压缩文件夹之前创建了新文件,我就想将计时器重置到该文件夹​​。

我使用FileSystemWatcher 来监控根文件夹及其子文件夹。

  1. 我不确定如何创建另一个监视程序来监视文件创建,可能是在 OnTimedEvent 方法中。
  2. 一旦检测到该文件夹​​的文件,我不知道如何重置计时器。我认为也是在 OnTimedEvent 中编写代码来重置它。

以下是我尝试的代码的一部分,可以在here 找到源代码。任何帮助将不胜感激。

    public class FileWatcher
    { 
     private FileSystemWatcher _watcherRoot;
     private Timer _timer;
     private readonly string _watchedPath;

    public FileWatcher(string path)
    {
        // _watcher = new FileSystemWatcher();
        _timer = new Timer();
        _watchedPath = path;


        InitWatcher();
    }

    public void InitWatcher()
    {
        _watcherRoot = new FileSystemWatcher();
        _watcherRoot.Path = _watchedPath;
        _watcherRoot.IncludeSubdirectories = true;
        _watcherRoot.EnableRaisingEvents = true;
        _watcherRoot.Created += new FileSystemEventHandler(OnCreated);

    }

    private void OnCreated(object sender, FileSystemEventArgs e)
    {

        if (e.ChangeType == WatcherChangeTypes.Created)
        {
            string fullPath = e.FullPath;
            if (sender == _watcherRoot)
            {
                // If detect new folder, set the timer to 5 sec
                _timer.Interval = 5000;
                _timer.Elapsed += OnTimedEvent;
                _timer.AutoReset = true;
                _timer.Enabled = true;

                // a directory
                Console.WriteLine($"{fullPath.ToString()} created on {DateTime.Now}");
            }

        }
    }

    private void OnTimedEvent(object sender, ElapsedEventArgs e)
    {
        // Create a 2nd Watcher??
        // Reset the timer in here??
    }

【问题讨论】:

  • 你为什么在这里使用定时器?
  • 想尝试在给定时间创建一个 zip。
  • 你想在这里做什么?在处理文件之前等待文件创建/修改停止?避免仍在写入的文件出现file locked 异常?在这两种情况下,您都需要跟踪 Created 和 Changed 事件。单个计时器可能不够。
  • @Patty_Putty 我再说一遍:你为什么在这里使用 timerst?您可以在任何时间、任何地点、任何地点进行压缩(只要权限可用)...那为什么要使用单个计时器?
  • 我尝试模拟一台机器来监控子文件夹中每个新文件的创建。问题是我无法判断文件的数量和大小是否在压缩之前等待它们完成。这就是为什么我需要设置一个计时器来观看它。????

标签: c# .net reset filesystemwatcher system.timers.timer


【解决方案1】:

这里有一个简单的扩展方法来重置给定的计时器。

  public static void Reset(this Timer timer)
    {
      timer.Stop();
      timer.Start();
    }

要从事件内部获取计时器对象,您需要将 sender 转换为 System.Timers.Timer() 或仅在静态上下文中使用计时器。

【讨论】:

    【解决方案2】:

    有一个名为Reactive Extensions 的非常聪明的库,最初由 Microsoft 编写为“Rx”,但现在放置在“System.Reactive”命名空间中。它允许您非常简单地编写复杂的事件驱动代码。

    例如,在您所描述的场景中,您可以对FileSystemWatcher 的事件“做出反应”并使用反应式“油门”,这意味着您只会在发生事件后收到通知该事件未发生的时间段。您还可以将多个不同的事件合并在一起。将这两个功能放在一起,然后为您的方法订阅它。

    如果这听起来像是一个可能的解决方案,您可能想看看Intro to Rx,这里有一个与解决此问题的方法有关的问题,包括各种答案中的大约 4 种方法:@ 987654323@(这不是那个问题的重复,因为你在问定时器,我建议你可能想使用反应式扩展)。

    【讨论】:

      【解决方案3】:

      我使用 lambda 表达式来解决这个问题,将计时器和观察者“绑定”在一起,这就是我发现的类似于 post

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多