473,513 Members | 2,505 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FileSystemWatcher + Multiple Events

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 file is completely
written. I understand there is nothing "exactly" like
that due to how the OS filesystem works, so I am looking
for a workaround.

I am currently attempting to open the "new" file in
exclusing mode to "test" if the file is complete from
within the OnCreate and OnChange event handlers. If the
exclusive open is not successful, I sleep the thread, and
try again. Works like a charm, except I get multiple
Change events thrown, which causes my "test" to be
executed multiple times for each file.

Does anyone have a workaround for this? Perhaps a way to
block/ignore the change events after the first for a
given file?

TIA.
Jul 21 '05 #1
3 9442
I'm in the same situation. That FileSystemWatcher is a real pain, isn't it?

The approach I take is similar to yours. I wait for the FileSystemWatcher to
fire an event when the FTP upload begins. I then turn OFF the
FileSystemWatcher and start firing a timer every 10 seconds to see when the
file is completely uploaded. I use the same technique that you do -- try to
open it in exclusive mode. Once the file is completely uploaded, I move the
file to a processing directory, kill the timer, and finally reactivate the
FileSystemWatcher.

David

"Michael Stanbrook" <ms********@yahoo.com> wrote in message
news:00****************************@phx.gbl...
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 file is completely
written. I understand there is nothing "exactly" like
that due to how the OS filesystem works, so I am looking
for a workaround.

I am currently attempting to open the "new" file in
exclusing mode to "test" if the file is complete from
within the OnCreate and OnChange event handlers. If the
exclusive open is not successful, I sleep the thread, and
try again. Works like a charm, except I get multiple
Change events thrown, which causes my "test" to be
executed multiple times for each file.

Does anyone have a workaround for this? Perhaps a way to
block/ignore the change events after the first for a
given file?

TIA.

Jul 21 '05 #2
David,

Whew, I thought I was the only one!

Are you actually using a timer? One of the other posts I
found on this topic suggested using a Thread.Sleep(xxx)
instead, and I'm wondering if there is much diference
betwen the two...

Thanks for your input..happy to hear I'm at least on the
right track.

Mike

-----Original Message-----
I'm in the same situation. That FileSystemWatcher is a real pain, isn't it?
The approach I take is similar to yours. I wait for the FileSystemWatcher tofire an event when the FTP upload begins. I then turn OFF theFileSystemWatcher and start firing a timer every 10 seconds to see when thefile is completely uploaded. I use the same technique that you do -- try toopen it in exclusive mode. Once the file is completely uploaded, I move thefile to a processing directory, kill the timer, and finally reactivate theFileSystemWatcher.

David

"Michael Stanbrook" <ms********@yahoo.com> wrote in messagenews:00****************************@phx.gbl...
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 file is completely written. I understand there is nothing "exactly" like
that due to how the OS filesystem works, so I am looking
for a workaround.

I am currently attempting to open the "new" file in
exclusing mode to "test" if the file is complete from
within the OnCreate and OnChange event handlers. If the
exclusive open is not successful, I sleep the thread, and try again. Works like a charm, except I get multiple
Change events thrown, which causes my "test" to be
executed multiple times for each file.

Does anyone have a workaround for this? Perhaps a way to
block/ignore the change events after the first for a
given file?

TIA.

.

Jul 21 '05 #3
> Whew, I thought I was the only one!

Are you actually using a timer? One of the other posts I
found on this topic suggested using a Thread.Sleep(xxx)
instead, and I'm wondering if there is much diference
betwen the two...


The vast majority of the time, Thread.Sleep() will work just as well as
a timer and Sleep() is easier to use and makes your code a bit easier to
follow too. The thing is, Sleep() blocks the calling thread. That's what
it's designed to do. In 99% of the cases this is no problem, but in a really
busy server process that is servicing tens of thousands of requests, it's
desirable to minimize the number of blocked threads. You see, the
FileWatcher ultimately fires its event from a "thread pool" thread. These
threads are a very valuable resource. There are only 25 of them per CPU. In
a situation where you have thousands of requests coming into your service
process, you don't want to waste one of your thread pool threads by blocking
(sleeping). Instead, it's better to set the timer and let that "thread pool"
thread return back to the pool so that it can service another request while
you're waiting for the timer to fire.

Unless your designing a high-load server app though, it usually makes
more sense to use Sleep() as you are doing.

David
Jul 21 '05 #4

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

Similar topics

0
2967
by: cxw0106 | last post by:
Hello, I have some weird problem with the FileSystemWatcher. I have developed an application that monitors a network directory for file changes of a certain file type. The program runs well for...
3
4583
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...
3
14224
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...
2
1972
by: kmcnet | last post by:
Hello Everyone and thanks for your help in advance. I have been battling a problem for nearly a month with the FileSystemWatcher component. Basically, what I am trying to do it to monitor three...
2
4534
by: DiGa | last post by:
Hi all I have to write an application in VB.NET which monitors multiple files at the time and notifies the user about changes. I thought to use multiple instances of FileSystemWatcher in an...
3
387
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...
1
10372
by: Abel Chan | last post by:
Hi there, I was trying to write a simple NT services using .NET 2.0 and fileSystemWatcher control. The goal is to poll documents from a watch directory and ftp them to a remote web site. I...
12
7417
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?
1
2641
by: D2 | last post by:
Hi, I have this problem when working with FileSystemWatcher class. I m using one of these objects to watch over a folder. Whenever i big file from another folder to the folder being watched, I...
0
7260
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
7161
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
7384
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,...
0
7539
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
7525
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...
1
5089
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
3222
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1596
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
456
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.