处理 FileSystemWatcher 中的多个更改事件

发布于 2024-07-10 05:41:47 字数 1073 浏览 5 评论 0原文

我有以下子内容:

  Private Sub Watcher_Changed(ByVal sender As System.Object, ByVal e As FileSystemEventArgs)
        If Path.GetExtension(e.Name) = ".p2p" Then
            Exit Sub
        Else
            Try
                ' multiple change events can be thrown. Check that file hasn't already been moved.
                While Not File.Exists(e.FullPath)
                    Exit Try
                End While

                ' throw further processing to a BackGroundWorker
                ChangedFullPath = e.FullPath
                ChangedFileName = e.Name

                FileMover = New BackgroundWorker
                AddHandler FileMover.DoWork, New DoWorkEventHandler(AddressOf ProcessFile)
                FileMover.RunWorkerAsync()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

        End If
    End Sub

当通过 FTP 上传文件时,我仍然收到多个文件更改通知。

我想修改 Try,这样如果更改通知发生在过去(时间)内(比方说 3 秒),它也会抛出更改通知。 这本来应该是微不足道的,但由于某种原因,我今天没有想到它,而且我的思绪也没有集中在我在谷歌上找到的答案上。

谢谢, 斯科特

I have the following sub:

  Private Sub Watcher_Changed(ByVal sender As System.Object, ByVal e As FileSystemEventArgs)
        If Path.GetExtension(e.Name) = ".p2p" Then
            Exit Sub
        Else
            Try
                ' multiple change events can be thrown. Check that file hasn't already been moved.
                While Not File.Exists(e.FullPath)
                    Exit Try
                End While

                ' throw further processing to a BackGroundWorker
                ChangedFullPath = e.FullPath
                ChangedFileName = e.Name

                FileMover = New BackgroundWorker
                AddHandler FileMover.DoWork, New DoWorkEventHandler(AddressOf ProcessFile)
                FileMover.RunWorkerAsync()
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

        End If
    End Sub

I'm still getting multiple changed-file notifications when a file is being uploaded by FTP.

I want to modify the Try so it also throws out the change notification if it has happened within the past (time) -- let's say 3 seconds. It ought to be trivial, but for some reason it isn't coming to me today, and my mind isn't wrapping itself around the answers I'm finding on Google.

Thanks,
Scott

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

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

发布评论

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

评论(7

情痴 2024-07-17 05:41:47

几年前,我创建了一项服务来上传转储到 FTP 文件夹的文件,以下是我所做的一些事情,应该可以帮助您解决问题:

  1. 我测试了所有 NotifyFilters,并选择了一个可以删除重复通知的过滤器(使用 仅NotifyFilters.Size为我创建的每个文件提供了可靠的通知,并消除了几乎所有重复的通知)
  2. 在Watcher_Changed事件中,我忽略了事件参数中包含的文件,只处理了文件夹中当前的所有文件; 我尝试将文件夹中的所有文件添加到队列中,如果它们已经在队列中,我会跳过下面的 3 个文件。
  3. 我为找到的每个独特的新文件创建一个新线程; 我的线程尝试获取文件上的锁,如果不能,则意味着其他进程仍在访问它(它仍在上传等),因此我的线程会休眠并在短时间间隔后再次尝试。
  4. 当文件被完全处理后,线程所做的最后一件事是将其移动到存档文件夹,然后将其从队列中删除,这样就不会意外地再次处理它。

这似乎对我来说很有效,并且该服务可靠地上传文件几个月,直到我离开那份工作。

I created a service to upload files dumped to an FTP folder a couple years ago and here's some things I did that should help you get over your problem:

  1. I tested all the NotifyFilters and chose the one that cut out duplicate notifications (using only NotifyFilters.Size gave me reliable notifications for every file created, and eliminated nearly all duplicate notifications)
  2. In the Watcher_Changed event I ignored the file included in the event args and just processed all files currently in the folder; I try to add all files in the folder to a queue, and if they're already in the queue, I skip 3 below.
  3. I spin off a new thread for every unique new file found; my thread tries to acquire a lock on the file, and if it can't that means some other process is still accessing it (it's still being uploaded, etc.) so my thread sleeps and tries again after a short interval.
  4. When a file is completely processed, the last thing the thread does is move it to an archive folder, and then remove it from the queue, so it isn't accidentally processed again.

This seemed to work out for me and that service ran reliably uploading files for months until I left that job.

如若梦似彩虹 2024-07-17 05:41:47

我们也有类似的情况; 然而,批量写入文件的系统使用了临时名称,并在完成后将其更改为“真实”名称。 你的 ftp 客户端可以做类似的事情吗?
如果可以,那么您可以检查新文件名并通过扩展名或前缀进行检查; 如果它不是预期的最终文件名格式,那么您可以忽略它。

we had a similar situation; however the system that wrote the file in batches used a temp name, and changed it to the "real" name when it was done. Can your ftp client do something similar?
If it can, then you can check the new filename and check it by extension or a prefix; if its not the expected final filename format then you can ignore it.

長街聽風 2024-07-17 05:41:47

我之前遇到过确切的情况,最终实现了一个计时器机制来等待文件“稳定”,或者等待写入事件停止进入 x 时间。

有点笨拙,但它有效。

I ran into the exact situation before, and ended up implementing a timer mechanism to wait for the file to "settle", or for write events to stop coming in for x amount of time.

A bit kludgy, but it worked.

最后的乘客 2024-07-17 05:41:47

需要注意的一件事是,访问文件可以更改文件属性之一,例如“访问时间”或其他内容。 如果您没有正确设置通知过滤器,则可能会触发另一个事件。

One thing to note is that accessing the file can change one of the file properties, like "accessed time" or something. That can then fire off another event if you don't have the notification filters setup right.

淡淡绿茶香 2024-07-17 05:41:47

我遇到了同样的问题,我正在保存一个通过代码上传的文件,删除和创建每次都会触发两次。 我所需要检查的是在进行处理之前该文件是否存在。

private void fsw_Created(object sender, FileSystemEventArgs e)
{
   if (File.Exists(e.FullPath))
   {
      //process the file here
   }
}

I had this same issue, I was saving a file that was uploaded through code and deleting and creating would fire twice each time. All I had to check was that the File was there before doing my processing.

private void fsw_Created(object sender, FileSystemEventArgs e)
{
   if (File.Exists(e.FullPath))
   {
      //process the file here
   }
}
爱的那么颓废 2024-07-17 05:41:47

我今天在尝试在更改时自动重新加载配置文件时遇到了同样的问题。 我最终计算了文件的 MD5 哈希值,以确定文件是否确实已更改,并且只有在哈希值不同时才重新加载。 这是我过滤掉重复项的唯一确定方法。

我短暂地考虑过使用计时器机制来过滤掉更改通知,但感觉就像 pete-m 一样,但我觉得这是一个太不干净的解决方案。

我尝试仅查看 LastWrite 时间更改,但这不起作用。 我仍然收到重复的通知。

I had the same problem today while trying to automatically reload a configuration file on change. I ended up computing an MD5 hash of the file to figure out if the file had actually changed and only reloading if the hash was different. This was the only definitive way I could filter out duplicates.

I briefly thought about using a timer mechanism to filter out change notifications but felt, like pete-m, but I felt it was too unclean a solution.

I tried watching only for LastWrite time changes but that didn't work. I was still getting duplicate notifications.

污味仙女 2024-07-17 05:41:47

我试图检测 Excel 文件何时发生更改,但遇到了一个问题,即更改的事件未触发,并且因为我只关心单个文件设置,所以过滤器不起作用。 当我删除过滤器并查看触发的事件时,事件参数名称属性看起来是随机生成的名称,而不是我知道已更改的特定文件名。

因此,当特定 Excel 文件发生更改时,我无法收到简单的通知。

I'm trying to detect when an excel file changes but am running into a problem that the changed event is not firing and as Im only concern with a single file setting the filter doesnt work. When I remove the filter and look at an event that is triggered the event argument name property looks to be a random generated name and not the specific filename which I know has changed.

So I'm not able to get a simple notification when a specific excel file changes.

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