爲什麼FileSystemWatcher激發兩次?有沒有簡單的方法來解決它?當然,如果我更新或編輯文本文件,它應該只會觸發一次?爲什麼FileSystemWatcher激發兩次
此鏈接這裏http://weblogs.asp.net/ashben/archive/2003/10/14/31773.aspx說
- 活動被兩次上調 - 事件將提高兩倍,如果事件處理程序(AddHander FSW.Created,AddressOf FSW_Created)是 明確指定。這是因爲默認情況下,公共事件 會自動調用相應的受保護方法(OnChanged, OnCreated,OnDeleted,OnRenamed)。要糾正這個問題,只需要刪除顯式事件處理程序(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
究竟您是否遇到此意外*行爲?是編輯文件,移動,刪除還是創建文件? – Arrow
嗨編輯該文件是什麼時候發生的 - 謝謝 –
我已經用可能的解決方案更新了我的問題,至少要檢查一下,因爲它可能有助於縮小問題的範圍。 – Arrow