Home All Groups Group Topic Archive Search About

File handles not being released

Author
29 Sep 2006 5:48 PM
ggruber
I have a Windows Service written in C# that acts as file transport function.
it moves files from a staging area to a production area for consumption by
third party software (Cognos Reporting).

since I must have exclusive access to these files (in the production area)
before I can copy new versions over them, we utilize another service that
'unhooks' file access so we can gain exclusive access to the files.

everything has been working fine until this week when we began to move files
in the 2 gig size area. no code changes have been made to account for the
behavior we are experiencing.

before I explain the problem, let me add one more wrinkle. the windows
service is synchronous, and single threaded. this design was intentional and
has been working well. since we have 3 realms (production, development, and
QA), each area is processed as a FIFO stack, with the service firing every 5
minutes to check for activity (new files in one of the staging areas).

occasionally we can not wait for the 15 minute cycle (5 min. X 3 realms) to
elapse -- we must move the files more quickly.

to accomplish this task, I designed an asynchronous multi-threaded process
(using the IAsyncResult interface and callbacks) to permit simultaneous
movement of files across all 3 realms at once.

Here’s the problem. If the Windows Service is stopped while it is in the
process of copying a file (especially a large one), it does not immediately
release its lock on the file handle it has acquired, even though all of the
objects are local, and are being properly closed.

This causes the secondary (asynchronous) process with a timeout error
because it cannot achieve a lock on the file handle that was released by its
predecessor.

While I was originally using File.Copy() to perform the copying process, I
have switched to using a binary FileStream to exercise more control over the
copying – and provide a delegate to display (on a separate UI thread) the
progress of the activity.

The $64 question is: how can I force the OS to release the file handle in a
timely manner? Am I the victim of write behind caching, unflushed buffers, or
something I am not aware of. Close() is not immediately releasing the handle.

AddThis Social Bookmark Button