Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsFree MagazinesWhite PapersSubmit Content
Discussion GroupsASP.NETWindows FormsLanguages.NET FrameworkVisual Studio.NET
Articles.NET FrameworkASP.NETToolsWindows Forms
.NET DirectoryOpen Source ProjectsUser GroupsWeb Resources
Related Topics
Visual Basic 6SQL ServerMS AccessOther DB ProductsMS Server ProductsMore Topics ...

.NET Forum / .NET Framework / New Users / April 2006

Tip: Looking for answers? Try searching our database.

detecting if a file is in use...

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Ben Callister - 07 Apr 2006 16:52 GMT
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
Kevin Spencer - 07 Apr 2006 21:45 GMT
try/catch

Signature

HTH,

Kevin Spencer
Microsoft MVP
Professional Numbskull

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

>i have an application that has a background thread that downloads files
>from a remote server and places them within a local directory.
[quoted text clipped - 26 lines]
> many thanks in advance,
> ben
Etienne - 07 Apr 2006 21:50 GMT
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
William Stacey [MVP] - 07 Apr 2006 23:07 GMT
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

Signature

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
[quoted text clipped - 24 lines]
| many thanks in advance,
| ben

Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.