473,396 Members | 1,834 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

FileSystemWatcher and Open Files

Hi NG,

I have a file driven application (a report server) which has to do some
work when new files arrive or are deleted.

When processing the notifications about newly created files from a
FileSystemWatcher I want to open the file and read some info from it.
Problem is that when opening the file directly when the event is fired I
usually get a "file used by another process".

Has anyone figured out a good way how to handle such events? Is there an
option to get a notification when the file is created and can be read?

My other alternatives I thought about, were:
1. Use a FIFO in a second thread that processes the files which are new.
Should keep iterating (with a short Thread.sleep) until all files are
imported successfully.
2. Start a worker thread from the pool for each file which tries to read
it in.
3. Forget about FileSystemWatcher and check the directory for new files
every 30 secs (Timer driven) [but how to reco if files have changed here?].

Does anyone has experience with this? Any best practices?

TIA,
Stefan
Aug 2 '06 #1
4 3689
Two tips I'm using, maybe they can help.

First one:

If you can control the sending application, it should first write the file
and then rename it.
Your app could listen then only rename changes. This way the file will be
readable when
you get the change event.
Like: sending app sends FILE.XXX and then renames it as FILE.TXT.
You intercept the rename and read it.

Second:

Use a semaphore file to indicate end of transmission. When you find create
event for the semaphore file,
scan the directory. Requires that you can control the sending app, of
course.

"Stefan L" <sl****@pp-software.REMOVEIfNoSpam.deha scritto nel messaggio
news:uD**************@TK2MSFTNGP02.phx.gbl...
Hi NG,

I have a file driven application (a report server) which has to do some
work when new files arrive or are deleted.

When processing the notifications about newly created files from a
FileSystemWatcher I want to open the file and read some info from it.
Problem is that when opening the file directly when the event is fired I
usually get a "file used by another process".

Has anyone figured out a good way how to handle such events? Is there an
option to get a notification when the file is created and can be read?

My other alternatives I thought about, were:
1. Use a FIFO in a second thread that processes the files which are new.
Should keep iterating (with a short Thread.sleep) until all files are
imported successfully.
2. Start a worker thread from the pool for each file which tries to read
it in.
3. Forget about FileSystemWatcher and check the directory for new files
every 30 secs (Timer driven) [but how to reco if files have changed
here?].

Does anyone has experience with this? Any best practices?

TIA,
Stefan

Aug 2 '06 #2
My problem is that I cannot control the sending app. The ReportServer
will be executed somewhere on a webserver and is intended to react to
files which will be copied (or modified or deleted) in a certain directory.
This can either be the explorer, some kind of FTP tool or whatsoever,
which will do the file work.

But I like your renaming idea better than the ones using file flags I
read when doing some research before I wrote to the group...

Laura T schrieb:
Two tips I'm using, maybe they can help.

First one:

If you can control the sending application, it should first write the file
and then rename it.
Your app could listen then only rename changes. This way the file will be
readable when
you get the change event.
Like: sending app sends FILE.XXX and then renames it as FILE.TXT.
You intercept the rename and read it.

Second:

Use a semaphore file to indicate end of transmission. When you find create
event for the semaphore file,
scan the directory. Requires that you can control the sending app, of
course.

"Stefan L" <sl****@pp-software.REMOVEIfNoSpam.deha scritto nel messaggio
news:uD**************@TK2MSFTNGP02.phx.gbl...
>>Hi NG,

I have a file driven application (a report server) which has to do some
work when new files arrive or are deleted.

When processing the notifications about newly created files from a
FileSystemWatcher I want to open the file and read some info from it.
Problem is that when opening the file directly when the event is fired I
usually get a "file used by another process".

Has anyone figured out a good way how to handle such events? Is there an
option to get a notification when the file is created and can be read?

My other alternatives I thought about, were:
1. Use a FIFO in a second thread that processes the files which are new.
Should keep iterating (with a short Thread.sleep) until all files are
imported successfully.
2. Start a worker thread from the pool for each file which tries to read
it in.
3. Forget about FileSystemWatcher and check the directory for new files
every 30 secs (Timer driven) [but how to reco if files have changed
here?].

Does anyone has experience with this? Any best practices?

TIA,
Stefan


Aug 2 '06 #3
Hi,

Has anyone figured out a good way how to handle such events? Is there an
option to get a notification when the file is created and can be read?
Nop, there is no way to know if you can access it unless you try and if you
get error is that you cannot access it :)
My other alternatives I thought about, were:
1. Use a FIFO in a second thread that processes the files which are new.
Should keep iterating (with a short Thread.sleep) until all files are
imported successfully.
This is an option, either with a Thread.Sleep or with a timer.
2. Start a worker thread from the pool for each file which tries to read
it in.
I prefer the first option.
3. Forget about FileSystemWatcher and check the directory for new files
every 30 secs (Timer driven) [but how to reco if files have changed
here?].
Does not solve the problem at all. Still the file is there but it;s in use
by another process.
If you can control the generation of the file you could create a "flag" file
and use this for the watcher. (see my other post about this)

If not possible then option 1 sounds good enough.
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
Aug 2 '06 #4
Hi !

I was in a similar situation. I just queued the "new file created" event
and a separate thread checks each file in the queue to be accessible
[open it with " FileMode.Open, FileAccess.ReadWrite, FileShare.None" to
be sure, it is ready processed and you are the only one, who can now
read it). After this test was successful, the item was removed from the
queue and a worker thread cretaed to do the final work.

May be, this helps.

Best regards,
Manfred

Stefan L wrote:
My problem is that I cannot control the sending app. The ReportServer
will be executed somewhere on a webserver and is intended to react to
files which will be copied (or modified or deleted) in a certain directory.
This can either be the explorer, some kind of FTP tool or whatsoever,
which will do the file work.

But I like your renaming idea better than the ones using file flags I
read when doing some research before I wrote to the group...

Laura T schrieb:
>Two tips I'm using, maybe they can help.

First one:

If you can control the sending application, it should first write the
file and then rename it.
Your app could listen then only rename changes. This way the file will
be readable when
you get the change event.
Like: sending app sends FILE.XXX and then renames it as FILE.TXT.
You intercept the rename and read it.

Second:

Use a semaphore file to indicate end of transmission. When you find
create event for the semaphore file,
scan the directory. Requires that you can control the sending app, of
course.

"Stefan L" <sl****@pp-software.REMOVEIfNoSpam.deha scritto nel
messaggio news:uD**************@TK2MSFTNGP02.phx.gbl...
>>Hi NG,

I have a file driven application (a report server) which has to do
some work when new files arrive or are deleted.

When processing the notifications about newly created files from a
FileSystemWatcher I want to open the file and read some info from it.
Problem is that when opening the file directly when the event is
fired I usually get a "file used by another process".

Has anyone figured out a good way how to handle such events? Is there
an option to get a notification when the file is created and can be
read?

My other alternatives I thought about, were:
1. Use a FIFO in a second thread that processes the files which are
new. Should keep iterating (with a short Thread.sleep) until all
files are imported successfully.
2. Start a worker thread from the pool for each file which tries to
read it in.
3. Forget about FileSystemWatcher and check the directory for new
files every 30 secs (Timer driven) [but how to reco if files have
changed here?].

Does anyone has experience with this? Any best practices?

TIA,
Stefan


Aug 2 '06 #5

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

Similar topics

4
by: Josh Usovsky | last post by:
I'm setting up a watched folder using FileSystemWatcher. When I drop a small file into the watched folder, I can respond to a .Created event and process the file with other code. However, if I try...
3
by: Michael Stanbrook | last post by:
I'm using the FileSystemWatcher to looks for new files arriving in a directory. Files are coming in via FTP and tend to be larger is size (> 1MB at least). I would like to fire an event once the...
2
by: Charlie Kunkel | last post by:
I need help. I have a directory I'm watching for creation of .TIF files, whereupon creation, I need to launch a process (command line exe) that converts the TIF file to a postscript file. (using...
2
by: Sagaert Johan | last post by:
Hi I use the FileSystemWatcher to detect creation of files. As soon as the event fires i do something with the file, but i sometimes get a sharing error. I noticed that when the event fires the...
1
by: TD | last post by:
I'm watching a folder for new files. When the files arrive, I want to wait until it is done being written to and then move it. Every time I move a file into the test folder, it triggers 3 calls...
3
by: Stampede | last post by:
Hi, I write an application which waits for incomming files in a specified directory. I thought, that using the FileSystemWatcher would be the best, as it does exactly what I need. But now I have...
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...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.