Results 1 to 6 of 6

Thread: Problem using FileSystemWatcher

  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.

  2. #2
    Sleep mode
    Join Date
    Aug 2002
    Location
    RUH
    Posts
    8,083
    See if this can help you .
    http://abstractvb.com/code.asp?A=1081

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    21
    thanx for the info.

    I've try using thread in Changed procedure:

    Code:
    #Region " Subs for Delegate "
        Private Sub Changed(ByVal sender As Object, ByVal e As System.IO.FileSystemEventArgs)
            Try
    
                Dim a As New System.Threading.ThreadStart(AddressOf ProcessFile)
            Dim b As New System.Threading.Thread(a)
            b.Start()
    
            Catch ex As Exception
                m_objLog.WriteEntry("[ Message= " & ex.Message & ":: Source= " & ex.Source & " ] at Changed", EventLogEntryType.Error)
            Finally
    
            End Try
        End Sub
    #End Region
    more stable than before.

    But I have one more problem:

    I want to be notified only when the file copy/transfer completed. Currently my code will notify whenever the folder added with new file - even if the file copy/transfer process not yet completed !

    Is there any solution to solve this problem ?

    thanx in advance.

    regards.

  4. #4
    Junior Member
    Join Date
    Oct 2005
    Posts
    18

    Re: Problem using FileSystemWatcher

    We have the same issue in our project. We are watching for new files using the filesystemwatcher however the file can take up to 1 minute to finish writing. The service does not process these files as a result. We have had to resort to writing the files (PDF) to a staging folder and then moving them into the watched folder. This is not ideal - we would like to eliminate this workaround. Any suggestions ?

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Dec 2003
    Posts
    21

    Re: Problem using FileSystemWatcher

    Last edited by bahruddina; Dec 25th, 2005 at 07:57 AM.

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: Problem using FileSystemWatcher

    I believe you'll find that if you create a FileInfo object and test the Attributes property. If it includes the Offline attribute then the file has not finished being created and you should not try to open it. You can use a loop and repeatedly test until the Offline attribute is not found. I'm not 100% sure but I think this will work, and if so it will avoid throwing and handling exceptions, which should always be avoided if possible.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

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