.net FileSystemWatcher如何触发多个更改只有一个
我有一个简单的测试应用程序:.net FileSystemWatcher如何触发多个更改只有一个,.net,vb.net,multithreading,filesystemwatcher,.net,Vb.net,Multithreading,Filesystemwatcher,我有一个简单的测试应用程序: Imports System.Diagnostics Public Class Form1 Public watchfolder As FileSystemWatcher Public Sub New() InitializeComponent() watchfolder = New System.IO.FileSystemWatcher("C:\\TEST") watchfolder.Notif
Imports System.Diagnostics
Public Class Form1
Public watchfolder As FileSystemWatcher
Public Sub New()
InitializeComponent()
watchfolder = New System.IO.FileSystemWatcher("C:\\TEST")
watchfolder.NotifyFilter = (NotifyFilters.DirectoryName Or NotifyFilters.FileName Or NotifyFilters.Size)
AddHandler watchfolder.Changed, AddressOf Zmiana
AddHandler watchfolder.Created, AddressOf Zmiana
AddHandler watchfolder.Deleted, AddressOf Zmiana
AddHandler watchfolder.Renamed, AddressOf Zmiana
watchfolder.EnableRaisingEvents = True
End Sub
Public Sub Zmiana()
If txtTest.InvokeRequired Then
txtTest.Invoke(Sub() txtTest.Text += "K")
Else
txtTest.Text += "K"
End If
End Sub
End Class
应用程序所做的是监视我的目录C:\Test中的更改
如果发生创建、删除、重命名、更改等更改,应用程序将向文本框写入字母“K”。它很好用。但是,例如,当我将5个文件复制到此目录时,事件会触发5次(文本框中显示5个字母)
我想做的是,当我将5个文件复制到该目录时,我只想引发一次事件(文本框中的一个字母“K”)。我怎样才能做到这一点呢?一个定时器可以轻而易举地完成这项工作。无论如何,您都需要一个,因为您很少能在事件发生时对文件执行任何操作@HansPassant在事件发生时很少能执行任何操作,因此您需要决定何时实际执行操作,是使用FileSystemWatcher事件还是使用计时器,因为文件写入不一定是瞬时的。另外,请记住,
Changed
事件可能会对同一文件触发多次。