OpenNETCF.IO.FileSystemWatcher 中多次发生的事件

发布于 2024-09-27 20:59:05 字数 418 浏览 2 评论 0原文

每当我订阅 FileSystemWatcher 通知时,当我创建新文件或更改现有文件时,都会发生多个事件。事件按以下顺序发生:

新文件

创建

已更改

已更改

更改

已更改

已更改已

更改更改

已更改已

删除

已更改

重命名

已重命名

删除

已删除

重命名和删除正在按预期工作。 Created 和 Changed 被多次调用。

添加/更改文件时是否有任何解决方案/解决方法可以获取准确的通知?

Whenever I am subscribing to FileSystemWatcher notification, multiple events are occuring when I create new file or change existing file. Events are occuring in following sequence:

New File

Created

Changed

Changed

Changed

Changed

Changed

Changed

Change

Changed

Deleted

Changed

Rename

Renamed

Delete

Deleted

Rename and Delete are working as expected. Created and Changed are invoked multiple time.

Is there any solution/workaround for getting exact notification when files are added/changed?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夏见 2024-10-04 20:59:05

如果您查看 FSW 的 SDF 源代码,您会发现它实际上是围绕本机 SHChangeNotifyRegister 调用的一个相当薄的托管填充程序,其中 dwEventMask 设置为 SHCNE_ALLEVENTS。窗口句柄被传递到 API,然后在发生更改时接收回调,并且这些回调被编组到 FSW 在托管端公开的托管事件。

现在查看回调,看起来有 9 个事件 ID 是句柄,其中四个引发 Changed 事件:

  • SHCNE_UPDATEDIR
  • SHCNE_RMDIR
  • SHCNE_UPDATEITEM
  • SHCNE_ATTRIBUTES

因此,当您创建新文件时,SHCNE_CREATE 为您提供 Created 事件,然后是其他一些事件引发多个 Changed 事件的回调。更改事件上的所有事件参数是否都相同?如果是,您必须使用调试器单步执行 SDF 代码,以准确查看传入的内容以及实际的回调。

简而言之,SDF 只是转发操作系统向其提供的事件。您看到所有这些事件的原因是操作系统发送它们。发送它们的确切原因可能是操作系统处理文件的方式,或者甚至可能特定于您正在使用的文件系统驱动程序(即在另一个设备上,它可能略有不同,甚至在另一个磁盘上)同一设备)。

我认为的解决方法是查看事件参数并对快速连续发生的相同文件名的事件进行“分组”。例如,如果您在一秒钟之内收到同一文件夹中同一文件名的一堆 Changed 和 Created 事件,那么很可能这是一个文件创建。

If you look at the SDF source code for the FSW, you'll see it's actually a pretty thin managed shim around the native SHChangeNotifyRegister call with the dwEventMask set to SHCNE_ALLEVENTS. A window handle is passed to the API that then receives callbacks when changes occur and those callbacks are marshalled to the managed events the FSW exposes on the managed side.

Now looking at the callback, it looks like there are 9 event IDs that are handles, four of which raise a Changed event:

  • SHCNE_UPDATEDIR
  • SHCNE_RMDIR
  • SHCNE_UPDATEITEM
  • SHCNE_ATTRIBUTES

So when you create a new file, it's SHCNE_CREATE gives you the Created event, followed by some other callbacks raising several Changed events. Are all of the event args the same on the change events? If they are, you'd have to use the debugger to step through the SDF code to see exactly what is coming in and for which actual callbacks.

The short story here is that the SDF is simply forwarding the events that the OS is giving to it. The reason you see all of these events is because the OS sending them. Exactly why it is sending them may be the way the OS is handling the file, or it may even be specific to the file system driver you're using (i.e. on another device it might be slightly different, or even on another disk in the same device).

The workaround I think would be to look at the event args and "group" the events for the same file name that happen in quick succession. For example if you get a bunch of Changed and a Created event for the same file name in the same folder all within a second, then it's a good bet that it was a file creation.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文