Multithreaded FileSystemwatcher

E

edgar Okioga

Hi,
Please answere me on my email :)

I have a socket server that monitors a linux directory for
file changes. These steps take place :

1) Once a new file is added to the directory, the
application reads the contents of the file which are name
of a client computer and some data.
2) Using the client computer name, the server then sends
the data to the respective client computer using TCP/IP
TCPsockets established at boot time.
3) The client then process the infromation by launcing a
16 bit application.
4) After which, the server moves the file to antoher
location.

Problem:
1) The server crashes when too many files are written to
the linux directory.
(I create a new thread for every file_Create event of the
filesystem watche. The thread uses async reads to parse
the file then closes the file, aborting the thread.

Here is some code:
'Handles all file creating events on the monitoersd
directory. Delegate called for evey
'new file
Private Sub FSWServer_Created(ByVal sender As
Object, ByVal e As System.IO.FileSystemEventArgs) Handles
FSWServer.Created
FilePath = e.FullPath
FileName = e.Name
'To avoid blocking, each call will result
to a new thread being written
'UpdateClients(e.FullPath)
ThreadIT = New Threading.Thread(AddressOf
ThreadSub)
ThreadIT.Start()
End Sub

'This sub is called by a new thread to handle the process
of reading form the
'file returned by all notifications. After read,
closes thread and aborts
Sub ThreadSub()
Try
'Add the new file to the listbox
'We get the File newly created and
move it to another location
'open the stream reader, read both
lines and move
If CheckFile(FilePath) Then
'move the file
'Dim Reader As
System.IO.StreamReader
Dim Reader As New
System.IO.FileStream(FilePath, IO.FileMode.Open)
Dim user As String
Dim Request As String
'Start asyn read of Data
Reader.BeginRead
(DataBuffer, 0, DataBuffer.Length, ReadFile, Reader)
'Try
' 'Reader =
System.IO.File.OpenText(Path)
' SyncLock Reader
' 'Read the
first line
' user =
Reader.ReadLine()
' Request =
Reader.ReadLine()
' 'Unicast
(user, "SEARCH|" & Request)
'
Reader.Close()
' End SyncLock
' Reader = Nothing
' UpdateClients(user
& "-" & Request)
'Catch ex As Exception
'
lstFileNotification.Items.Clear()
' UpdateNotification
("Error->" & Path)
' txtError.Text
= "Error->" & Now().ToShortTimeString & "-" & ex.Message
'Finally
' 'Close the object
if it has been created
' If Not Reader Is
Nothing Then
'
Reader.Close()
' Reader =
Nothing
' 'move the
file to another location
System.IO.File.Move
(FilePath, lblMonitor.Text & "\" & FileName)
'
'UpdateClients(user & "-" & Request)
'End If
'End Try
End If
Catch ex As Exception
FSWServer.EnableRaisingEvents =
False
lstFileNotification.Items.Clear()
UpdateNotification("Directory
Notification Error")
txtError.Text = "Error->" & Now
().ToShortTimeString & "-" & ex.Message
End Try
ThreadIT.Abort()
ThreadIT = Nothing
End Sub

'Once a read is done, call back is called async method
'thus not stopping client application
Sub ReadFileCallBack(ByVal ar As IAsyncResult)
Dim fs As System.IO.FileStream
Dim BytesRead As Integer
Dim strMessage As String
Dim Data() As String
Try
fs = CType(ar.AsyncState(),
System.IO.FileStream)
'Stop other threads from reading
SyncLock fs
BytesRead = fs.EndRead(ar)
'Read data from async call
strMessage =
System.Text.ASCIIEncoding.ASCII.GetString(DataBuffer, 0,
BytesRead)
fs.Close()
End SyncLock
Catch ex As Exception
End Try
'Since the data is read in binary form,
split using the vbcrlf
Data = Split(strMessage, Constants.vbCrLf,
2, CompareMethod.Binary)
'update the screen
UpdateClients(Data(0) & "-" & Data(1))
'Send data to single client
Unicast(Data(0), "SEARCH|" & Data(1))
Application.DoEvents()
End Sub

Please reply as I am desparate!

Cheers
 
M

Microsoft News

I've been doing some reading and the problem is that a process is still
writing to the directory when the application wants to move the file to a
new location. The FileSystemwater is monitoring FileName

Any hints on how to determine that a file is not being processed? More so,
writing has ended? I've come across use of a timer, threadpool e.tc, what is
the best practice pattern I can follow?


Cheers :)
 

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