[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 920
  • Last Modified:

Files deleted in C# Windows Service aren't deleted until service is stopped

I have a C# Windows Service that periodically polls a folder and compares its contents to another folder.  If necessary, it deletes files.  The problem I'm having is that the file does not actually get deleted until the service is stopped.  After the attempted deletion, the service can no longer read that file's attributes.

Whenever the service attempts to access the file after attempting to delete it, an "access is denied" exception is raised.  When I run Sysinternal's Process Monitor, it reports "DELETE PENDING" when that exception is raised.

I'm accessing the file's extended metadata through a third party component.  That component's code is unavailable to me.  I've got a feeling that the service is not releasing file handles for some reason, even though I can't determine where.  My guess is that it is that component.

My question is this: Short of shutting the program down, how can I force it to release these file handles?  Is it simply a matter of clearing out (nullifying) the objects that access the metadata, or is there something I can do at the application (service) level?  Is this related to the garbage collector (GC)?

Thanks in advance for your advice.
0
OfficeOps
Asked:
OfficeOps
  • 4
  • 2
1 Solution
 
athapaCEO / CTOCommented:
What 3rd party component is it? May be you can substitute the component with a better on.
You can try unlocking that file using Unlocker. It has command line you can use in your app. If you are doing a lot of delete then starting every process could be an issue but this program is fast and reliable than others I've seen. It is free too.
0
 
OfficeOpsAuthor Commented:
Thanks for the quick response, athapa.  I'm using the DLL named "Toub.MediaCenter.Dvrms.dll" provided by Stephen Toub for reading the metadata of the DVR-MS files created by Windows Media Center.  DVR-MS is a wrapper for the video file that allows Media Center to store additional metadata related to recorded TV shows.  The project can be found at the end of a rather lengthy blog post found here:

http://blogs.msdn.com/toub/archive/2007/09/22/fun-with-dvr-ms.aspx

I have to correct myself, too.  I do have access to the source code.

I'd rather not rely on an external program to release the file handles that I'm creating with my own.  There has to be a better way to do this (or fix my code), not to mention the effect that Unlocker would have on my running service.

Thanks for your help!
0
 
OfficeOpsAuthor Commented:
Oh, specifically I'm using the GetAttributes function of the Toub.MediaCenter.Dvrms.Metadata.DvrmsMetadataEditor object in the project I mentioned in my previous post.  See the code snippet below.  The code works fine, but I don't think that the file ever gets closed after the metadata is read.
using System.IO;
using Toub.MediaCenter.Dvrms.Metadata;
 
FileInfo file = new FileInfo("c:\somefolder\somefile.dvr-ms");
 
DvrmsMetadataEditor meta = new DvrmsMetadataEditor(file.FullName);
Hashtable attribs = (Hashtable)meta.GetAttributes();

Open in new window

0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
OfficeOpsAuthor Commented:
I suppose it serves me right.  After digging into the source code, I found what I needed to find the solution.  For those of you who care, I had to amend my code as shown below.  The DvrmsMetadataEditor was using a COM object that, unless explicitly disposed of, was never closed until the service was shut down.  Mr. Toub even offered a utility class to deal with this.

After putting my code into a using block referring to his utility code, I'm free and clear.

Sorry for wasting everyone's time!
using System.IO;
using Toub.MediaCenter.Dvrms.Metadata;
using Toub.MediaCenter.Dvrms.Utilities;
 
<snip/>
 
private DisposalCleanup _dc;
protected DisposalCleanup DisposalCleanup { get { return _dc; } }
 
<snip/>
 
FileInfo file = new FileInfo("c:\somefolder\somefile.dvr-ms");
 
DvrmsMetadataEditor meta;
Hashtable attribs;
 
using (_dc = new DisposalCleanup())
{
    meta = new DvrmsMetadataEditor(file.FullName);
    _dc.Add(meta);
 
    attribs = (Hashtable)meta.GetAttributes();
}

Open in new window

0
 
athapaCEO / CTOCommented:
This may not be an issue but it might be a good idea to see if you are slowly leaking any handles or objects because of the COM object. See if the dispose (or other part of the code) is cleaning up all of the references of COM objects using ReleaseComObject (until the reference count is 0).
0
 
OfficeOpsAuthor Commented:
It's not an issue here, but good point.  When the DisposalCleanup object is disposed at the end of the code block, its dispose message runs the code listed in the snippet below.

We're good there, and now my service runs swimmingly.  Thanks for your help!
while (Marshal.ReleaseComObject(toDispose) > 0);

Open in new window

0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now