gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 07:25:18 25 4
gpt4 key购买 nike

为什么 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com