Results 1 to 6 of 6

Thread: Problem using FileSystemWatcher

Threaded View

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    21

    Resolved Problem using FileSystemWatcher

    Hi all,

    I'm using FileSystemWatcher to monitor folder for changes( new file added). This application is developed as a Windows Services.

    The problem is - sometimes it cannot detect the changes that occurred on the folder (new file added).

    Is there any tips that I can use to solve this problem...PLEASE ?

    Below are the codes from my application:


    declarations:
    Code:
    #Region " Private Variables "
    
        Private m_objLog As EventLog
        Private m_objWatcher As IO.FileSystemWatcher
    
        Dim gFTPDirectory As String
        Dim gFTPFilePrefix As String
        Dim gQTFSPath As String
    
        Dim gobjTProcessStart As New System.Threading.ThreadStart(AddressOf ProcessFilesAtStartUp)
        Dim gobjTProcess As New System.Threading.Thread(gobjTProcessStart)
    
    #End Region
    When service is started:
    Code:
    Protected Overrides Sub OnStart(ByVal args() As String)
    
            Try
    
                'initialize new event log
                m_objLog = New EventLog()
                'check if log already exists
                If Not m_objLog.SourceExists("ExaQFile2QTFS") Then
                    m_objLog.CreateEventSource("ExaQFile2QTFS", "Application")
                End If
                m_objLog.Source = "ExaQFile2QTFS"
    
                'read the setting and get the tag list
                ReadINIFile()
    
                'initialize Watcher
                m_objWatcher = New IO.FileSystemWatcher()
    
                'Set Path to Watch
                m_objWatcher.Path = gFTPDirectory
                'Set Filter to *.*
                m_objWatcher.Filter = gFTPFilePrefix & "*.txt"
                'Watch Subdirectories
                m_objWatcher.IncludeSubdirectories = False
                'Set Changes to be watched
                m_objWatcher.NotifyFilter = IO.NotifyFilters.FileName
    
                'Pointer on Sub for events
                AddHandler m_objWatcher.Created, AddressOf Changed
    
                'Start Watching
                m_objWatcher.EnableRaisingEvents = True
    
                'prepare for system start up...thread
                gobjTProcess.IsBackground = True
                gobjTProcess.Start()
    
            Catch e As Exception
                m_objLog.WriteEntry("[ Message= " & e.Message & ":: Source= " & e.Source & " ] at OnStart", EventLogEntryType.Error)
            Finally
    
            End Try
    
        End Sub
    code when folder changed:
    Code:
    #Region " Subs for Delegate "
        Private Sub Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
            Try
    
                ProcessFiles()
    
            Catch ex As Exception
                m_objLog.WriteEntry("[ Message= " & ex.Message & ":: Source= " & ex.Source & " ] at Changed", EventLogEntryType.Error)
            Finally
    
            End Try
        End Sub
    #End Region
    when service is stoped:
    Code:
    Protected Overrides Sub OnStop()
    
            Try
                m_objLog.Dispose()
                m_objWatcher.Dispose()
            Catch e As Exception
                m_objLog.WriteEntry("[ Message= " & e.Message & ":: Source= " & e.Source & " ] at OnStop", EventLogEntryType.Error)
            End Try
    
        End Sub
    Please feel free to see my code.......

    Thank you very very much in advance for the kindness help and tips.

    Regards.
    Attached Files Attached Files
    Last edited by bahruddina; Dec 24th, 2005 at 08:10 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width