Why isn't my FileSystemWatcher events fireing ?

K

Kai Thorsrud

Hi i'm an event Noob and i'm calling a class from my main module console app
like this. I'm sorry if this is a lot of code to read but i can't see the
error according to my book.
Thanks a LOT.

if there are other noobs here like me feel free to use my code if it's of
any use :)

I've set a breakpoint at doWorkOnChangedFile but notinh happens... i'm
flooding my log like hell.


Dim oFileWatcher As New sysLogIoHandler
oFileWatcher.FileNamePathWithOutEndingSlash = "C:\Program
Files\Syslogd\Logs"
oFileWatcher.FileName = "SyslogCatchAll.txt"
oFileWatcher.Constructor()
Do While 1 < 2
System.Threading.Thread.Sleep(400)
Loop


And here is some code from my class. I call the Sub Constructor now for
debugging purposes.

'-----------------------------------------
Class sysLogIoHandler code
'-----------------------------------------

Public Sub Constructor()

Dim oFileStream As System.IO.File
' Ensure that the file supports the properties we are to use later
on and then set the EOF position of the File
' Check if the file exists
sCompleteFileName = sFilePath & "\" & sFileName

If File.Exists(sCompleteFileName) = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "File does not
exists or no access to the file :" & sFileName, EventLogEntryType.Error)
Exit Sub
End If

' Try to open a FileStream
Try
oWatcherFileSystemStream = (oFileStream.Open(sCompleteFileName,
FileMode.Open, FileAccess.Read, FileShare.None))
Catch ex As Exception
oWatcherEventLogger.LogEvent(Me.EventLogName, ex.ToString,
EventLogEntryType.Error)
End Try

' Check if the fileStream supports seeking
If oWatcherFileSystemStream.CanSeek = False Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "The filestream
does not support Seeking" & sCompleteFileName, EventLogEntryType.Error)
Exit Sub
End If

'Set the EOF of the file property since we are to handle all
changes from here on.
lEofPosition = getCurrentEOF()
doWatchFileForChanges()

End Sub


Private Function getStreamsCurrentEOF() As Long
getStreamsCurrentEOF = CLng(oWatcherFileSystemStream.Length)

End Function

Public Function doWatchFileForChanges() As Boolean
Dim oWatcherFileWatcher As New FileSystemWatcher
'Set the path we are to watch
oWatcherFileWatcher.Path = sFilePath
'Set the Filter to just watch one file
oWatcherFileWatcher.Filter = sFileName
'Watch for increase in size or change of lastwrittenflag
oWatcherFileWatcher.NotifyFilter = NotifyFilters.LastWrite Or
NotifyFilters.Size
'Set a handler to the events we want to handle
AddHandler oWatcherFileWatcher.Changed, AddressOf
doWorkOnChangedFile
AddHandler oWatcherFileWatcher.Deleted, AddressOf
doWorkOnChangedFile

' Tell the FileWatcher to start working and raise events
oWatcherFileWatcher.EnableRaisingEvents = True

End Function
Private Sub doWorkOnChangedFile(ByVal source As Object, ByVal e As
System.IO.FileSystemEventArgs)
Select Case e.ChangeType
Case WatcherChangeTypes.Changed
Console.Write(doReadChangesInFile)
Case WatcherChangeTypes.Deleted
Console.Write("The file was deleted")
End Select
End Sub

Private Sub doNotifyThatFileIsNoMore(ByVal source As Object, ByVal e As
System.Io.FileSystemEventArgs)

End Sub

Private Function doReadChangesInFile() As String
If Me.getCurrentEOF = Me.getLastKnownEOF Then
oWatcherEventLogger.LogEvent(Me.EventLogName, "A 'Changed File
Event' Triggered and I'm trying to find the change but the filesize is the
same as lasttime i did this" & , EventLogEntryType.Error)
Return "" ' Return with no Data
End If
Dim iNumberOfBytes As Integer
' Get the number of bytes we are supposed to read
iNumberOfBytes = Me.getCurrentEOF - Me.getLastKnownEOF
' Prepare Buffer to read data into with the correct size
Dim abReaderBuffer(iNumberOfBytes) As Byte
' GoTo the last known EOF before the last change
oWatcherFileSystemStream.Seek(Me.getLastKnownEOF, SeekOrigin.Begin)
'Read the data into our buffer
oWatcherFileSystemStream.Read(abReaderBuffer, 0, iNumberOfBytes)
' set the next knownEOF since we have handled the changed data
lEofPosition = getCurrentEOF()
Return Encoding.ASCII.GetString(abReaderBuffer)
End Function
 
C

CJ Taylor

damn.. was hoping for NT, which FSW can't watch on... so lets actually look
into yoru code. =)

Kai Thorsrud said:
Windows XP Pro. It's supposed to run on Windows 2003 server when done





HERE IT IS.

you declare your FileWatcher in the wrong context. Don't declare its type
within a method, but declare it within the class

to make things easier you would declare it WithEvents, so you have

Public WithEvents oWatcher as FileSystemWatch

then in your method below

oWatcher = new FilesystemWatcher()
 
C

CJ Taylor

Whoops, forogto to tell you my comment for your fix is inline...(scroll down
in other message)

see previous post.
 
K

Kai Thorsrud

haha you really made my day.... Thanks a lot CJ.... are you the same CJ that
used to hang out on irc #windowsNT supporting ppl ? (I used to hang out
there a lot back in 1997 i don't remember my nick thou)

Beaming a cold beer right back to you (damn core dump... oh well in a few
years maybe)

/Kai
 
K

Kai Thorsrud

Thanks for your help CJ (Are you the same CJ that used to hang on
#windowsnt on irc back in 1997 ?)

Beaming a beer to you in return (damn... core dump... hmm damn debug)

However it still doesn't raise events... reading like hell over here...
I also tried adding this line after EnableRaisingEvents

oWatcherFileWatcher.EnableRaisingEvents = True
oWatcherFileWatcher.WaitForChanged(WatcherChangeTypes.All)

but WaitForChanged never returns...
 
C

CJ Taylor

Are you using an actual event handler? I mean this is in the nicest way
possible, but do you understand event driven programming?

WaitForChanged only works on the last fired event. it doesn't actually
notify. It kinda works like a timer.

what you want to do is have aother method in there that is like this


WaitForChanged is basically
Kai Thorsrud said:
Thanks for your help CJ (Are you the same CJ that used to hang on
#windowsnt on irc back in 1997 ?)

Beaming a beer to you in return (damn... core dump... hmm damn debug)

However it still doesn't raise events... reading like hell over here...
I also tried adding this line after EnableRaisingEvents

oWatcherFileWatcher.EnableRaisingEvents = True
oWatcherFileWatcher.WaitForChanged(WatcherChangeTypes.All)

but WaitForChanged never returns...

Private Shared Sub OnChanged(source As Object, e As FileSystemEventArgs) _
handles oWatcherFileWatcher.Created,
oWatcherFileWatcher.Changed, oWatcherFileWatch.Deleted, _
oWatcherFileWatcher.Renamed
' Specify what is done when a file is changed, created, or deleted.
Console.WriteLine("File: " & e.FullPath & " " & e.ChangeType)
End Sub

put a breakpoint on that one to see when the file watcher changes...
Asyncronous programming =)
 
C

CJ Taylor

Ahh yes, and as for windowsnt, yeah I did sometimes, but I remember a lot of
people did... a lot of people are named CJ too . =)
 
K

Kai Thorsrud

Hi again!

I just came home from work and i've been playing around with the code for a
while... It turns out there is nothing wrong with my code...
I changed the code to monitor the entire directory and it triggers if i have
a text file in the directory and i open the text file in textpad and change
that file....

However it wont trigger on my Kiwi Syslog Deamon's log file ( The
SyslogDeamon is running and windows explorer updates the logfile's
attributes everytime the logfile get's updated).
The logfile has a lock on it. i can open the logfile in textpad but i can't
write to it since there is a write lock on it...

Is this a limitation of the FileSystemWatcher ? If so is there a way around
this ? Darn fck'in stupid FileSystemWatcher if it is so.... The windows
Explorer sure notify the changes in the file and update's it's size and file
attributes...

/Kai
 
K

Kai Thorsrud

damn stupid me... i have solved it... in my contructor i also open this log
file for reading to have a stream ready for the event which is supposed to
read the changes in the log file :D
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top