vb.net - 为什么 FileSystemWatcher 触发两次

标签 vb.net visual-studio-2010 filesystemwatcher

为什么 FileSystemWatcher 触发两次?有没有简单的方法来修复它?当然,如果我更新或编辑文本文件,它应该只触发一次吗?

这个链接在这里http://weblogs.asp.net/ashben/archive/2003/10/14/31773.aspx

  1. Events being raised twice - An event will be raised twice if an event handler (AddHander FSW.Created, AddressOf FSW_Created) is explicitly specified. This is because, by default, the public events automatically call the respective protected methods (OnChanged, OnCreated, OnDeleted, OnRenamed). To correct this problem, simply remove the explicit event handler (AddHandler ...).


“删除显式事件处理程序”是什么意思?
Imports System.IO

Public Class Form2

    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed

        'this fires twice
        MessageBox.Show("test")

    End Sub

    Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FileSystemWatcher1.Path = "C:\Users\c\Desktop\test\"
        FileSystemWatcher1.NotifyFilter = NotifyFilters.LastAccess Or NotifyFilters.LastWrite Or NotifyFilters.FileName Or NotifyFilters.DirectoryName Or NotifyFilters.CreationTime

        FileSystemWatcher1.IncludeSubdirectories = False
        FileSystemWatcher1.Filter = "text.txt"

    End Sub

End Class

最佳答案

更新:

我想出了2个解决方案。一个使用线程,另一个不使用。随意选择:-)。

无线程:

Imports System.IO

Public Class Form1
    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
        Dim watcher As System.IO.FileSystemWatcher = sender
        watcher.EnableRaisingEvents = False

        'Do work here while new events are not being raised.
        MessageBox.Show("Test")

        watcher.EnableRaisingEvents = True 'Now we can begin watching for new events.

    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FileSystemWatcher1.Path = "C:\Users\c\Desktop\test"
        FileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite
        FileSystemWatcher1.IncludeSubdirectories = False
        FileSystemWatcher1.Filter = "test.txt"


    End Sub

    Private Sub FileSystemWatcher_OnChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

End Class

此解决方案(无线程)将 watcher.EnableRaisingEvents 设置为 False。在此之后,您通常会处理任何受影响(或更改)的文件。然后在您的工作完成后将 EnableRaisingEvents 设置回 True。

带线程:
Imports System.IO

Public Class Form1
    Private Sub FileSystemWatcher1_Changed(ByVal sender As System.Object, ByVal e As System.IO.FileSystemEventArgs) Handles FileSystemWatcher1.Changed
        FileSystemWatcher1.EnableRaisingEvents = False
        Threading.Thread.Sleep(250)
        FileSystemWatcher1.EnableRaisingEvents = True


        MessageBox.Show("test")


    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        FileSystemWatcher1.Path = "C:\Users\c\Desktop\test"
        FileSystemWatcher1.NotifyFilter = NotifyFilters.LastWrite
        FileSystemWatcher1.IncludeSubdirectories = False
        FileSystemWatcher1.Filter = "test.txt"


    End Sub

    Private Sub FileSystemWatcher_OnChanged(ByVal sender As System.Object, ByVal e As System.EventArgs)

    End Sub

End Class

这个解决方案虽然有点笨拙,但确实有效。它基于您不需要每 250 毫秒检查一次更改的假设,在 250 毫秒内禁用检查新的更改/事件,然后重新启用检查。我已经尝试了几乎所有我能想到的方法来为您找到真正的解决方案,但这同时会起作用。

关于vb.net - 为什么 FileSystemWatcher 触发两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12940516/

相关文章:

c# - 如何在C#中实现多行字符串; VB 的 XML 文字的替代品?

VB.Net DataTable 的嵌套关系如何?

c# - 从程序集中提取文字字符串

visual-studio - 为什么服务器资源管理器添加连接对话框中缺少 SQLite 提供程序?

c# - 尽管采取了措施,FileSystemWatcher 事件仍会引发两次

VB.Net 自定义 Minecraft 启动器

VB.NET 在文本框中查找特定文本

visual-studio - Visual Studio 2010 beta 1 是否可用?

c++ - 有没有人在 C++/WinAPI 中有类似 FileSystemWatcher 的类?

vb.net - 文件系统观察器不工作