473,398 Members | 2,427 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,398 software developers and data experts.

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 9424
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
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:
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,...
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...
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
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...

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.