473,473 Members | 1,754 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Multithreaded FileSystemwatcher

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(DataBuff er, 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

Nov 20 '05 #1
1 4670
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 :)

"edgar Okioga" <ed**********@undp.org> wrote in message
news:06****************************@phx.gbl...
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(DataBuff er, 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

Nov 20 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: pradyumna | last post by:
In Project settins - C/C++ - Code Generation, what is the difference between the option "Multithreaded" and "Multithreaded DLL". I understand that on selecting multithreaded option, single and...
6
by: Dan Kelley | last post by:
We have a multithreaded app that responds to events, and writes these events to a text file. This text file is used by an external system for further processing. We want to be able to write...
1
by: Troy Murphy | last post by:
How do I prevent the FileSystemWatcher event to keep firing while the file is being created? When copying a file to the watched folder, the event fires a dozen or more times! Also, the...
7
by: Allen Anderson | last post by:
I'm trying to figure out a way to catch when a file has been written to a directory. I currently have it where I can catch when the file begins writing, but this isn't helpful as I need to know...
2
by: Jet Leung | last post by:
Hi all, I had made a program to watching files in my directory. I had used a instance of FileSystemWatcher to do my work.And I had add some events of the FileSystemWatcher , for example onChange,...
13
by: David | last post by:
I have been working on trying to write a directory watcher service. One of the requirments is that it be able to watch multiple directories, not sub directories of one parent directory, but just...
3
by: Stampede | last post by:
Hi, I want to use the FileSystemWatcher in a Windows Service. I read an article, where the author created the FileSystemWatcher object in a seperate thread and when the event is fired, he started...
20
by: J-T | last post by:
We are working on an asp.net application which is a 3-tier application.I was aksed to create a component which monitors a folder and gets the file and pass them to a class library in our business...
12
by: ljh | last post by:
Has anyone else noticed that the FileSystemWatcher raises the changed event twice when a file is changed? Do you have any idea why this is the case?
5
by: =?Utf-8?B?Sm9obiBT?= | last post by:
I am trying to find out if there is a way to tell if there is already a filesystemwatcher (created by a webservice) monitoring a folder. I have a webservice that creates a filesystemwatcher,...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.