[英]Use timer with fileSystemWatcher in C#
方案是我有一个根文件夹来监视任何新文件夹(包含文件)并设置一个计时器来单独压缩每个文件夹。 但是,在调用zip函数之前,我无法判断文件夹中的文件是否是最后一个文件,因此,只要在压缩文件夹之前创建了新文件,我就想将计时器重置为该文件夹。
我使用FileSystemWatcher
来监视根文件夹及其子文件夹。
下面是我尝试过的代码的一部分,源代码可以在这里找到。 任何帮助将受到高度赞赏。
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??
}
这里有一个简单的扩展方法来重置给定的计时器。
public static void Reset(this Timer timer)
{
timer.Stop();
timer.Start();
}
要从事件内部获取计时器对象,您需要将sender
System.Timers.Timer()
为System.Timers.Timer()
或者只在静态上下文中使用计时器。
有一个非常聪明的库Reactive Extensions ,最初由Microsoft编写为“Rx”,但现在放在“System.Reactive”命名空间中。 它允许您非常简单地编写复杂的事件驱动代码。
例如,在您正在描述的场景中,您可以对FileSystemWatcher
的事件做出“反应”并使用Reactive“Throttle”,这意味着只有在一段时间之后才会收到事件的通知。事件没有发生。 您还可以将多个不同的事件合并在一起。 将这两个功能放在一起,并订阅您的方法。
如果这听起来像是一个可能的解决方案,你可能想看看Rx的Intro ,这里有一个与解决这个问题的方法有关的问题,包括在各种答案中做到这一点的4种方法: 包装文件观察器在反应式扩展中 (这不是该问题的重复,因为你问的是Timers,我建议你可能想要使用Reactive Extensions)。
我使用lambda表达式来解决这个问题,将计时器和观察者“绑定”在一起,这就是我发现的类似于这篇文章的内容 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.