Smart questions
Smart answers
Smart people
Join Tek-Tips Forums

Member Login

Remember Me
Forgot Password?
Join Us!

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips now!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

Join Tek-Tips
*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.
Jobs from Indeed

Link To This Forum!

Partner Button
Add Stickiness To Your Site By Linking To This Professionally Managed Technical Forum.
Just copy and paste the
code below into your site.

File Sysytem Watcher - multiple events being raised

bmpsu (Programmer) (OP)
22 Feb 08 12:07
I originally posted in the forum and realized that this should probably be in this forum. Sorry for reposting here.

Original post - thread855-1450101: FileSystemWatcher - AddHandler

I have been away from this code for a few days. I am back working on this.  I have tried a few different variations of the code below. I just can't seem to come up with a solution to only raise the change and create events once on a single file. In my searching on the web, this seems to be a common issue. I have not seen a true solution to this.  Has anyone come across this.

Is there a way to check and see if a file has finished copying to a folder? It seems that when a file is copied to my watch directory, multiple events are raised during this process which triggers multiple attempts to copy my file.

Public Sub StartWatcher()

    Dim fwService As FileSystemWatcher
    Dim gblpvFilters As String() = {"*.zip", "*.txt", "*.dat", "*.csv"}

        For Each filter As String In gblpvFilters

           fwService = New FileSystemWatcher()
           fwService.Path = "C:\myFileWatcherTest\"
           fwService.IncludeSubdirectories = True
           'fwService.NotifyFilter = (NotifyFilters.LastWrite)
           fwService.Filter = filter

           AddHandler fwService.Changed, AddressOf MoveFile
           AddHandler fwService.Created, AddressOf MoveFile

           fwService.EnableRaisingEvents = True

End Sub

Private Sub MoveFile(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        File.Copy(e.FullPath, "C:\move\" & e.Name, True)
End Sub
bmpsu (Programmer) (OP)
22 Feb 08 12:20
I also should have mentioned that this code is broken down for posting purposes. I do have this code in a windows service application. I thought this code would display what I have and trying to do. If anyone wants the all of my code, I can post that. (But this time in the code blocks.)
bmpsu (Programmer) (OP)
2 Apr 08 14:45
So I find out that the file system watcher component is responding as it should. There are multiple events fired when a transaction like this occurs. The trick is to find a way to ignore/avoid the multiple events. This is what I came up with below.

I do have a problem I hope someone can help me out with. For no reasons I can see or catch in this code, my service stops watching. The service remains running and throws no exceptions or errors of any kind; but periodically with no rhyme or reason - stops. To make it work again, simply restart the service, then it works fine. I need this to run always.

The onTimedEvent() Sub always returns False for both conditions. I am not sure if this is why it stops watching my folders or not. That is the only thing I have been able to come up with. Do I have some threading issues?

Please Help!


Public Class FileWatcherService

    'Global page variables
    Private gblpvPathToWatch As StringCollection = My.Settings.PathToWatch
    Private gblpvFilters As StringCollection = My.Settings.Filters
    Private gblpvMoveTo As StringCollection = My.Settings.MoveTo
    Private gblpvCopyTimeSpan As Integer = My.Settings.CopyTimeSpan
    Private gblpvEventsTimer As Integer = My.Settings.EventsTimer

    Private gblpvFSWThread As Thread
    Private ht As Hashtable
    Private aTimer As New System.Timers.Timer()

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        ht = New Hashtable

        'add a timer to check status
        aTimer.Interval = gblpvEventsTimer
        aTimer.Enabled = True
        AddHandler aTimer.Elapsed, AddressOf OnTimedEvent
    End Sub

    Protected Overrides Sub OnStart(ByVal args() As String)
    End Sub

    Private Sub Watch()
        gblpvFSWThread = New Thread(AddressOf StartMonitor)
        gblpvFSWThread.Priority = ThreadPriority.Lowest
    End Sub

    'Build FileSystemWatcher
    Private Sub StartWatch()
            For Each path As String In gblpvPathToWatch
                Me.fswComponent = New FileSystemWatcher()
                Me.fswComponent.Path = path.Trim
                Me.fswComponent.NotifyFilter = (NotifyFilters.CreationTime Or _
                                                NotifyFilters.FileName Or _
                                                NotifyFilters.LastAccess Or _
                                                NotifyFilters.LastWrite Or _
                Me.fswComponent.IncludeSubdirectories = True
                Me.fswComponent.Filter = "*.*"

                AddHandler Me.fswComponent.Changed, AddressOf MoveFile
                AddHandler Me.fswComponent.Created, AddressOf MoveFile
                AddHandler Me.fswComponent.Error, AddressOf WatcherError

                Me.fswComponent.EnableRaisingEvents = True
        Catch ex As Exception
        End Try
    End Sub

    Private Sub WatcherError(ByVal source As Object, ByVal e As System.IO.ErrorEventArgs)
        'We need to create new version of the object because the old one is now corrupted
        Dim watchException As Exception = e.GetException()
        Me.fswComponent.EnableRaisingEvents = False
    End Sub

    Private Sub MoveFile(ByVal source As Object, ByVal e As System.IO.FileSystemEventArgs)
        Dim aSourceSplit As String() = Nothing
        Dim strSourceFolder As String = Nothing
        Dim strDestDirectory As String = Nothing
        Dim strFileMoveTo As String = Nothing
        Dim ChangeTypeAccepted As Boolean = False

        'start monitoring for the next file (was missing files before?)

        If IsExtValid(e.FullPath) Then 'checks to see if file is correct extension
            If IsFileLocked(e.FullPath) = False Then 'tries to open file

                'loop through each gblMoveTo folder and copy the file to this folder
                For Each moveToPath As String In gblpvMoveTo
                    strFileMoveTo = moveToPath & e.Name

                    If HasBeenMoved(strFileMoveTo) = False Then
                        ht.Add(strFileMoveTo, DateTime.Now.ToString)
                        File.Copy(e.FullPath, strFileMoveTo, True)
                    End If

            End If
        End If

        'this ends the activites of this thread and leaves the new watcher running.
    End Sub

    'check to see if file has been moved within the gblpvCopyTimeSpan value
    Private Function HasBeenMoved(ByVal filePath As String) As Boolean
        Dim result As Boolean = False
        Dim mSeconds As Double
        Dim dtNow As DateTime = DateTime.Now
        Dim fileNameDate As DateTime = Nothing

            If ht.ContainsKey(filePath) Then
                fileNameDate = CType(ht.Item(filePath), DateTime)
                mSeconds = dtNow.Subtract(fileNameDate).TotalMilliseconds

                If mSeconds > gblpvCopyTimeSpan Then
                    result = True
                End If
            End If
        Catch ex As Exception
            result = False
            If Not ht Is Nothing Then
            End If
        End Try

        Return result
    End Function

    'Checks to see if file exists and if file has a valid extention from config
    Private Function IsExtValid(ByVal filePath As String) As Boolean
        Dim result As Boolean = False

        If File.Exists(filePath) Then
            For Each filter As String In gblpvFilters
                If Path.GetExtension(filePath).ToLower = filter.Trim.ToLower Then
                    result = True
                    Exit For
                End If
        End If

        Return result
    End Function

    'Trys to open file. If fails then file is still being written to ftp directory and returns false.
    Private Function IsFileLocked(ByVal filePath As String) As Boolean
        Dim fs As FileStream = Nothing
        Dim result As Boolean = False

            fs = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.None)
        Catch ex As Exception
            result = True
            If Not fs Is Nothing Then
            End If
        End Try

        Return result
    End Function

    Private Sub OnTimedEvent(ByVal source As Object, ByVal e As ElapsedEventArgs)
        If (gblpvFSWThread.IsAlive = False) AndAlso hasRestarted = False Then
            'Check to see if the thread is alive
            Me.fswComponent.EnableRaisingEvents = False
        ElseIf ((gblpvFSWThread.ThreadState And ThreadState.Unstarted) <> ThreadState.Running) AndAlso hasRestarted = False Then
            'Check to see if thread is running
            Me.fswComponent.EnableRaisingEvents = False
        End If
    End Sub
bmpsu (Programmer) (OP)
2 Apr 08 17:34
Looks like I might be experiencing a term called "race condition - when two or more threads perform an operation, and the result of the operation depends on unpredictable timing factors; when each thread completes the operation".

This is all very new to me, so I am going to sleep on it.  Anyone familiar with this and is this my problem?  Or am I way off base?  This seems to logically explain why it stops watching and giving me no thrown errors or exceptions.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Back To Forum

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close