在FileSystemWatcher中处理多个变更事件 [英] Handling multiple Change Events in FileSystemWatcher

查看:61
本文介绍了在FileSystemWatcher中处理多个变更事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下内容:

  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上传文件时,我仍然收到多个更改文件通知.

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

我想修改尝试",因此如果它在过去(时间)内发生(例如3秒钟),它还会抛出更改通知.它本来应该是微不足道的,但是由于某种原因,它今天并没有出现在我眼前,我的思想也没有将自己包裹在我在Google上找到的答案周围.

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.

谢谢, 斯科特

推荐答案

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

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. 我测试了所有的NotifyFilters并选择了一个可以删除重复通知的通知(仅使用 NotifyFilters.Size为我创建的每个文件提供了可靠的通知,并消除了几乎所有重复的通知)
  2. 在Watcher_Changed事件中,我忽略了事件args中包含的文件,而只处理了该文件夹中当前的所有文件;我尝试将文件夹中的所有文件添加到队列中,如果它们已经在队列中,则跳过下面的3.
  3. 对于找到的每个唯一新文件,我都会派出一个新线程;我的线程试图获取对该文件的锁定,如果不能锁定,则意味着其他进程仍在访问该文件(该文件仍在上载等),因此我的线程进入睡眠状态,并在很短的间隔后再次尝试.
  4. 文件完全处理完后,线程所做的最后一件事就是将其移至存档文件夹,然后将其从队列中删除,因此不会再次被意外处理.
  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.

这篇关于在FileSystemWatcher中处理多个变更事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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