FileSystemWatcher的改变事件(" LastWrite")是靠不住的 [英] FileSystemWatcher changed event (for "LastWrite") is unreliable

查看:255
本文介绍了FileSystemWatcher的改变事件(" LastWrite")是靠不住的的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要当一个文件在磁盘上的更新,以得到一个通知。我感兴趣的是作为一个刷新后尽快得到这个通知,但是,似乎当流打开或关闭FileSystemWatcher对象将只发送一个事件。

I am trying to get a notification when a file is updated on disk. I am interested in getting this notifications as soon as a flush occurs, yet, it seems that the FileSystemWatcher would only send an event when the stream opens or closes.

在下面的代码,我正在写一个文件,并多次刷新缓冲区到磁盘。然而,FileSystemWatcher的只是在写的开始通知我一次,一次,当他们结束了。

In the code below I am writing to a file and repeatedly flush the buffer to the disk. Yet, the FileSystemWatcher only notified me once in the beginning of the writes and once when they end.

有另一种方式来获得这些通知? ?或者我应该使用轮询为

Is there another way to get these notifications? Or should I use polling for that?

中的代码:

class Program
{
    static void Main(string[] args)
    {
        FileSystemWatcher watcher = new FileSystemWatcher(Environment.CurrentDirectory, "Test.txt");
        watcher.Changed += watcher_Changed;
        watcher.EnableRaisingEvents = true;

        using(TextWriter writer = new StreamWriter("Test.txt"))
        {
            WriteData(writer);
            WriteData(writer);
            WriteData(writer);
            WriteData(writer);
        }

        Thread.Sleep(10000);
    }

    private static void WriteData(TextWriter writer)
    {
        writer.WriteLine("Hello!");
        writer.Flush();
        Console.WriteLine(DateTime.Now.ToString("T") + "] Wrote data");
        Thread.Sleep(3000);
    }

    static void watcher_Changed(object sender, FileSystemEventArgs e)
    {
        Console.WriteLine(DateTime.Now.ToString("T") + "] Watcher changed!");
    }
}



更新1



我已经纠正了日期时间的ToString函数来显示秒。下面是与上面的代码的输出:

UPDATE 1

I've corrected the ToString function of the DateTime to show seconds. Here is the output with the code above:

11:37:47 AM] Watcher changed!
11:37:47 AM] Wrote data
11:37:50 AM] Wrote data
11:37:53 AM] Wrote data
11:37:56 AM] Wrote data
11:37:59 AM] Watcher changed!



谢谢!

Thanks!

推荐答案

它无关的FileSystemWatcher的。观察者反应对文件系统的属性LastWrite更新。

It has nothing to do with the FileSystemWatcher. The watcher reacts to updates on the LastWrite attribute for the filesystem.

例如。 NTFS不会在每次写入更新LastWrite。值被缓存,仅当流被关闭或在一些其它未指定的时间写入。 这份文件说

E.g. NTFS does not update the LastWrite on every write. The value is cached and only written when the stream is closed or at some other unspecified time. This document says

时间戳是在不同的时间和因各种原因更新。关于文件的时间戳的唯一保证是,当使更改关闭句柄文件时间正确反映。 [...] NTFS文件系统延迟更新最后访问的时间的最后访问

Timestamps are updated at various times and for various reasons. The only guarantee about a file timestamp is that the file time is correctly reflected when the handle that makes the change is closed. [...] The NTFS file system delays updates to the last access time for a file by up to 1 hour after the last access

我假设一个类似的缓存适用于写

I assume a similar caching applies for write

这篇关于FileSystemWatcher的改变事件(" LastWrite")是靠不住的的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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