I have a filewatcher program that is working syncronously (which is how I
want it to work).
But my program will handle whatever files are in the folder at the time it
runs. So when I drop 15 files in the folder at once, the event kicks of 15
times - even though all the files were processed in the 1st event.
This is fine, it just won't find any files in the folder for 14 times.
But if I drop 600 files in the folder at once, it will do about 590+ looks
into an empty folder.
I assume (and we all know what "assume" is) that the filewatcher object is
keeping an array or collection of files that get moved into the folder and
process this list one at a time (runs my program once for each event).
Is there a way to tell filewatcher after I process the 1st event to just
clear it's list or array so it doesn't keep running my program when there is
nothing in my folder?
I was thinking of doing something that would stop filewatcher from looking
at the folder (something like: fileWatcher.EnableRaisingEvents = False)
while I am processing it and then when done (fileWatcher.EnableRaisingEvents
= True) to start watching the folder. But this wouldn't clear out the list
it has so it would still process the 500 events (I think).
My code is:
************************************************** *****************
Imports System.IO
Imports FieldNameMapSetup
Public Class ServiceApp
Private fileWatcher As FileSystemWatcher
Private fieldNameSetup As FieldNameMapSetup.FieldNameMapSetup
Protected Overrides Sub OnStart(ByVal args() As String)
Try
System.Diagnostics.EventLog.WriteEntry(Me.ServiceN ame,
Me.ServiceName + " started.", _
System.Diagnostics.EventLogEntryType.Information)
'Use the following line to slow down the onstart to allow you to
attach to the process.
'System.Threading.Thread.Sleep(15000)
InitializeFileWatcher()
Catch ex As Exception
MyException.Publish(ex)
End Try
End Sub
Protected Overrides Sub OnStop()
System.Diagnostics.EventLog.WriteEntry(Me.ServiceN ame, Me.ServiceName
+ " stopped.", System.Diagnostics.EventLogEntryType.Information)
End Sub
Private Sub InitializeFileWatcher()
fileWatcher = New FileSystemWatcher()
fileWatcher.Path = MySettings.XMLFilePath
fileWatcher.NotifyFilter = NotifyFilters.FileName
fileWatcher.Filter = "*.*"
AddHandler fileWatcher.Created, AddressOf HandleAppraisals
fileWatcher.EnableRaisingEvents = True
End Sub
Private Sub HandleAppraisals(ByVal o As Object, ByVal e As
FileSystemEventArgs)
Try
fieldNameSetup = New FieldNameMapSetup.FieldNameMapSetup()
fieldNameSetup.CheckForAppraisals()
Catch exc As Exception
fieldNameSetup.MoveFiles(FieldNameMapSetup.FieldNa meMapSetup.fileInProcess,
FieldNameMapSetup.MySettings.ExceptionFilePath)
Logging.WriteToLog(MySettings.LogFilePath, exc.Message + "Writing
to: " + FieldNameMapSetup.MuSettings.ExceptionFilePath + _
Path.GetFileName(FieldNameMapSetup.FieldNameMapSet up.fileInProcess))
End Try
End Sub
End Class
************************************************** ******************
Thanks,
Tom