[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1137
  • Last Modified:

FileSystemWatcher Fires OnChanged Event Multiple times

I have an .xml file that I am using as a configuration file that is loaded during application startup.  I have created a FileSystemWatcher object and set it to filter the FileName and LastWrite .xml file to filter on Changed and Rename events.  When the file is renamed, a message is popped and the application is terminated.  If the file is edited and saved I call the subs to reload the .xml file.  The rename changetype event operates correctly - it fires only once.  However when the file is edited and saved, the Changed event is fired three times.  I have coded around the problem but am not confident in whether it will always work as intended - why three times and will it always be three times on all computers? Can't find anything on M$ site about the potential cause of this!

Code extract follows:

Public Module Startup
    '
    Private intChanged As Integer = 0
    '
    Public Sub Main()
        ...code
        ' Create FileSystemWatcher and set properties
        Dim watcher As New FileSystemWatcher
        watcher.Path = Application.StartupPath
        ' Watch for changes in LastWrite times and file rename
        watcher.NotifyFilter = NotifyFilters.FileName Or NotifyFilters.LastWrite
        'Only watch the app config file
        watcher.Filter = "Config.xml"
        AddHandler watcher.Changed, AddressOf OnChanged
        AddHandler watcher.Deleted, AddressOf OnChanged
        AddHandler watcher.Renamed, AddressOf OnRenamed
        ' Begin watching.
        watcher.EnableRaisingEvents = True
        LoadConfig(Application.StartupPath & "\" & Config.xml)
       '.... load the GUI
    End Sub

    Private Sub OnChanged(ByVal source As Object, ByVal e As FileSystemEventArgs)
        ' Execute when file is changed.
        intChanged += 1
       ' Msgbox(Cstr(e.ChangeType) & " event fired " & intChanged & " time(s)") ' For info
        If intChanged = 3 Then
        intChanged = 0
            LoadConfig(e.FullPath)  'Sub to reload the .xml file
        End If
    End Sub

    Private Sub OnRenamed(ByVal source As Object, ByVal e As RenamedEventArgs)
        ' Exit the application if the config file is renamed
        MsgBox("File: " & e.OldName & " was renamed to " & e.Name & "!" & _
            vbCrLf & "This file is a required application file.  The " & _
            "application will terminate.", MsgBoxStyle.Critical, "File System Error")
        Application.Exit()
        '
    End Sub

    Private Sub LoadConfig(strXMLFile as String)
        'Xml file loading & parsing stubs
    End Sub

End Module
0
FlurbSnarf
Asked:
FlurbSnarf
1 Solution
 
Mike TomlinsonMiddle School Assistant TeacherCommented:
From the help on the FileSystemWatcher:

    "Note  Common file system operations might raise more than one event. For example, when a file is moved from one directory to another, several OnChanged and some OnCreated and OnDeleted events might be raised. Moving a file is a complex operation that consists of multiple simple operations, therefore raising multiple events. Likewise, some applications (for example, anti-virus software) might cause additional file system events that are detected by FileSystemWatcher."

In answer to your question:

>> I have coded around the problem but am not confident in whether it will always work as intended - why three times and will it always be three times on all computers?

So you can't be sure of the number of times it will fire.  I'm not sure of the best way to handle this other than to reload the file each time it fires.
0

Featured Post

Upgrade your Question Security!

Add Premium security features to your question to ensure its privacy or anonymity. Learn more about your ability to control Question Security today.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now