problem with file handles

  • Thread starter Thread starter SharpCoderMP
  • Start date Start date
S

SharpCoderMP

hi,

in my app i monitor the filesystem for changes with FileSystemWatchers.
When the change is detected the app performs some actions using Shell32
to obtain information from the filesystem. now the problem is that
apparently the CLR not always closes the file handles immediately.
this is rather annoying to the user because he's unable to change
(write, delete, rename) these locked files or directories. the strangest
thing is that some handles are closed at once and some not - this is
totally random. sometimes it takes few minutes before the handle is
closed even if my app does not need those files anymore.

so the question is, how can i force my app to close those unused file
handles?
 
SharpCoderMP said:
hi,

in my app i monitor the filesystem for changes with FileSystemWatchers.
When the change is detected the app performs some actions using Shell32
to obtain information from the filesystem. now the problem is that
apparently the CLR not always closes the file handles immediately.
this is rather annoying to the user because he's unable to change
(write, delete, rename) these locked files or directories. the strangest
thing is that some handles are closed at once and some not - this is
totally random. sometimes it takes few minutes before the handle is
closed even if my app does not need those files anymore.

so the question is, how can i force my app to close those unused file
handles?

You are saying that you are calling the Close method and the system is
not releasing the file?

Chris
 
Chris said:
You are saying that you are calling the Close method and the system is
not releasing the file?

no it's not like that. i have a code like this (this is only example -
it is not my actual code):

Shell32.Folder shell32Folder = SOME_PLACE_TO_START;
foreach (Shell32.FolderItem item in shell32Folder.Items())
{
this.myFileList.Add(item);
}

/* the Shell32 namespace is available
* when you reference the system's Shell32.dll
* the VS IDE will generate a wrapper for it */

as you can see i do not perform any direct file operations. i only use
Shell32 to access some folder data. that's it. what's more the
Shell32.FolderItem does not provide any methods to close the eventual
file handle. the problem is that some file handles that apparently CLR
uses are not closed immediately. note that some handles *are* closed
just after they are used and some remain open for some time. it is
totally random which handles are closed and which not. eventually after
some random amount of time all of the remaining handles are closed. the
problem is that this can take as short as only few seconds or as long as
few minutes - it's just unpredictable. and during that time this files
(or folders) are locked - they cannot be renamed, moved or deleted from
outside of my app.

so is there any way i can force CLR to close those unused file handles?
 

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

Back
Top