detecting if a file is in use...

B

Ben Callister

i have an application that has a background thread that downloads files from
a remote server and places them within a local directory. additionally, i
have another thread that is running the FileSystemWatcher provided by .NET
which notifies me when a file is created in that directory and then spins
off another worker thread to perform some final processing on the new file.

my problem is that my final worker thread that processes the file CANNOT
begin to process the file UNTIL the file has been FULLY downloaded. yet i
receive the event from FileSystemWatcher early when the file first appears
(but often is still being written to).

so, i was wondering if:

1) is there a way to determine if a file is in use (e.g. file handle count)?
if so, then my worker thread can just wait until the file is no longer in
use?
2) if not, can someone suggest a better alternative implementation to
solving this issue?

*please note that i cannot have the thread that is performing the download
actually trigger the final worker thread itself when it is done writing the
file to disk. this is because i also have the requirement that if a file is
simply copied (e.g. by the user) from another location on the local
computer, that it be picked up and processed by the FileSystemWatcher as
well. therefore, i must use this FileSystemWatcher-based implementation.

many thanks in advance,
ben
 
K

Kevin Spencer

try/catch

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

Show me your certification without works,
and I'll show my certification
*by* my works.
 
E

Etienne

Hi,
As I understand what you want to do, you could have the "download"
module notify the "watcher" that it's about to download a file. The
watcher module could then filter out it's watch on the file being
downloaded until the download module notify that the file is completely
downloaded.
Best Regards,
Etienne Lebeau
 
W

William Stacey [MVP]

It is really the same problem in both cases. The Create event only says the
directory *entry was created, not that it is done and closed. So you still
need to wait for a close on a copy operation or a download as the stream is
still opened in either case. So, you may want to refactor your protocol a
little.

All new files, both copy and download, goto a temp dir under your watch dir.
When the file is done, you move (i.e. rename) the file into your "watched"
dir (make sure DIRs are in same partition, othewise it would involve a copy
across partitions and you would be back to first issue). Now just Watch for
the Renamed event in your file system watcher and do your thing. hth

--
William Stacey [MVP]

|i have an application that has a background thread that downloads files
from
| a remote server and places them within a local directory. additionally, i
| have another thread that is running the FileSystemWatcher provided by .NET
| which notifies me when a file is created in that directory and then spins
| off another worker thread to perform some final processing on the new
file.
|
| my problem is that my final worker thread that processes the file CANNOT
| begin to process the file UNTIL the file has been FULLY downloaded. yet i
| receive the event from FileSystemWatcher early when the file first appears
| (but often is still being written to).
|
| so, i was wondering if:
|
| 1) is there a way to determine if a file is in use (e.g. file handle
count)?
| if so, then my worker thread can just wait until the file is no longer in
| use?
| 2) if not, can someone suggest a better alternative implementation to
| solving this issue?
|
| *please note that i cannot have the thread that is performing the download
| actually trigger the final worker thread itself when it is done writing
the
| file to disk. this is because i also have the requirement that if a file
is
| simply copied (e.g. by the user) from another location on the local
| computer, that it be picked up and processed by the FileSystemWatcher as
| well. therefore, i must use this FileSystemWatcher-based implementation.
|
| many thanks in advance,
| ben
|
|
|
 

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