如何检测文件的正确修改? [英] How detect the correct modification of a file?

查看:181
本文介绍了如何检测文件的正确修改?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

上一个问题中,我的一个问题是我的应用程序如何知道文件已被重写。我应该补充:通过另一个应用程序。首先,我尝试了 ShellNotify ,它报告目录中的更改,但不包含特定文件中的更改afaik,我错过了什么?



现在我在一个计时器中使用FileAge,每秒钟检查一个特定的文件是否已经改变。虽然这样做,它检测到两个修改,相隔约13毫秒;当我将测试间隔更改为10秒时,此行为仍然存在。下面的代码是定时器的回调函数。
$ b $ $ $ $ $ $ $ $ $ $ $> $ T $
var new_stamp:TDateTime;如果(new_stamp<> FDateTimeStamp)则
begin
FDateTimeStamp:= new_stamp;
FTask.SyncCall(notify_user);
end; //如果
结束; //如果
结束; // doWork //

我的问题:


  1. 究竟是什么导致了 notify_user 在上面的代码中被调用了两次?愚蠢的错误或文件系统的一些奇怪的行为(例如修改日期是在打开和关闭文件时设置的)?
  2. 检查每一秒是否是浪费一个文件已经改变。是否有像ShellNotify那样只在特定文件被修改时才通知我?

更新大卫Heffeman(使用 ReadDirectoryChangesW 而不是轮询)的答案是问题2的正确答案。但是,它导致以及在问题1中提到的一个修改(从用户的角度来看)中的几个调用。

我比较了 ReadDirectoryChangesW 和轮询方法。测试两个文件(0.5MB和10 MB)。在这两种情况下,轮询方法一直被调用两次, ReadDirectoryChangesW 被多次调用(3到5次)。我将恢复到轮询方法,并忽略第一个 FileAge 更改。我知道我不能确定这种行为是否一致,因为我不明白造成这种情况的原因。

这不是直接回答你的问题,但我会建议使用 ReadDirectoryChangesW 而不是轮询。


In a previous question one of my questions was how my application could know that a file has been rewritten. I should have added: "by another application". First I tried ShellNotify which reports changes in a directory but not in a specific file afaik, am I missing something?

Now I use FileAge in a timer that checks each second whether a specific file has changed. While that works it detects two modifications, about 13 ms apart; this behavior persists when I change the testing interval to 10 seconds. The code below is the callback of the timer.

procedure TAMI_Column_Selector.doWork (Sender: TObject);
var new_stamp: TDateTime;
begin
   if FileAge (remove_extension (FileName) + '.csv', new_stamp) then
   begin
      if (new_stamp <> FDateTimeStamp) then
      begin
         FDateTimeStamp := new_stamp;
         FTask.SyncCall (notify_user);
      end; // if
   end; // if
end; // doWork //

My questions:

  1. What exactly does cause the notify_user to be called twice in the code above? A dumb mistake or some strange behavior of the file system (e.g. the modification date is set on opening and on closing the file)?
  2. It feels kind of "wasteful" to check each second whether a file has changed. Is there something like ShellNotify that notifies me only when a specific file has been modified?

Update

The answer of David Heffeman (use ReadDirectoryChangesW instead of polling) is a correct answer for question 2. However, it leads as well to several calls during one modification (from the users point of view) as mentioned in question 1.

I compared ReadDirectoryChangesW and the polling method. Tested with two files (0.5MB and 10 MB). In both cases the polling method was called consistently twice and the ReadDirectoryChangesW was called several times (3 to 5 times). I'll revert to the polling method and ignore the first FileAge change. I know I cannot be sure that this behavior will be consistent as I don't understand the mechanism causing it.

解决方案

I know this is not a direct answer to your question, but I would recommend using ReadDirectoryChangesW rather than polling.

这篇关于如何检测文件的正确修改?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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