H
hooksie2
Hi,
I am trying to use FileSystemWatcher to watch a log file which is
written to via a 3rd party app and display the log in listbox on a
form. The FileSystemWatch seems to work okay but I get the following
error when I try to write to the listbox:
"A first chance exception of type 'System.ArgumentNullException'
occurred in System.Windows.Forms.dll"
the ins and outs of threading. This was meant to be a nice simple
project to learn from so I'd really appreciate any tips.
My form has a run button which calls the following routine (in a
separate module):
Private mstrPathToWatch As String
Private mstrFileToWatch As String
Private mfswWatchFolder As FileSystemWatcher
Private frm As frmMain
Public Sub StartFileWatch(ByVal frmLogForm As frmMain)
mfswWatchFolder = New FileSystemWatcher
mfswWatchFolder.SynchronizingObject = frmLogForm.lbxRunLog
'this was just a guess but I don't understand it and it
doesn't seem to help
'Create module level reference to form so can access it from
changed event
frm = frmLogForm
'Specify the path we want to watch
mstrPathToWatch = "C:\Documents and Settings\Andrew\My
Documents\TEMP"
mstrFileToWatch = "demo.txt"
mfswWatchFolder.Path = mstrPathToWatch
mfswWatchFolder.Filter = mstrFileToWatch
'Add a list of Filter we want to specify
mfswWatchFolder.NotifyFilter = IO.NotifyFilters.DirectoryName
mfswWatchFolder.NotifyFilter = mfswWatchFolder.NotifyFilter Or
IO.NotifyFilters.FileName
mfswWatchFolder.NotifyFilter = mfswWatchFolder.NotifyFilter Or
IO.NotifyFilters.Attributes
'Add a handler for each event (in this case just file change)
AddHandler mfswWatchFolder.Changed, AddressOf FileHasChanged
'Set this property to true to start watching
mfswWatchFolder.EnableRaisingEvents = True
End Sub
The 'FileHasChanged' event created above reads the log file
(successfully) and then calls a sub back on the form which writes the
array to the listbox.
Thanks a lot!
Andrew
I am trying to use FileSystemWatcher to watch a log file which is
written to via a 3rd party app and display the log in listbox on a
form. The FileSystemWatch seems to work okay but I get the following
error when I try to write to the listbox:
"A first chance exception of type 'System.ArgumentNullException'
occurred in System.Windows.Forms.dll"
being in a different thread but to be honest I haven't really figuredFrom my search so far it seems this may be to do with the watcher
the ins and outs of threading. This was meant to be a nice simple
project to learn from so I'd really appreciate any tips.
My form has a run button which calls the following routine (in a
separate module):
Private mstrPathToWatch As String
Private mstrFileToWatch As String
Private mfswWatchFolder As FileSystemWatcher
Private frm As frmMain
Public Sub StartFileWatch(ByVal frmLogForm As frmMain)
mfswWatchFolder = New FileSystemWatcher
mfswWatchFolder.SynchronizingObject = frmLogForm.lbxRunLog
'this was just a guess but I don't understand it and it
doesn't seem to help
'Create module level reference to form so can access it from
changed event
frm = frmLogForm
'Specify the path we want to watch
mstrPathToWatch = "C:\Documents and Settings\Andrew\My
Documents\TEMP"
mstrFileToWatch = "demo.txt"
mfswWatchFolder.Path = mstrPathToWatch
mfswWatchFolder.Filter = mstrFileToWatch
'Add a list of Filter we want to specify
mfswWatchFolder.NotifyFilter = IO.NotifyFilters.DirectoryName
mfswWatchFolder.NotifyFilter = mfswWatchFolder.NotifyFilter Or
IO.NotifyFilters.FileName
mfswWatchFolder.NotifyFilter = mfswWatchFolder.NotifyFilter Or
IO.NotifyFilters.Attributes
'Add a handler for each event (in this case just file change)
AddHandler mfswWatchFolder.Changed, AddressOf FileHasChanged
'Set this property to true to start watching
mfswWatchFolder.EnableRaisingEvents = True
End Sub
The 'FileHasChanged' event created above reads the log file
(successfully) and then calls a sub back on the form which writes the
array to the listbox.
Thanks a lot!
Andrew