Reputation: 730
I have a backgroundworker
thread in my c#
application that extracts files from a compressed file.
I notice that the backgroundworker
does complete.
If I then attempt to delete the directory where the files where extracted to, by the backgroundworker
, I get an IOException
, telling me that the directory and files are in use.
I used processexplorer.exe
and the process is my C# application. I attempted to call and ensure the backgroundworker
was .CancelAsync()
and .Dispose()
. I don't understand why the handle is still on the files?
If I close my app and start it up again, the files are deleted upon startup from the temp location they were extracted to, as I have code upon startup of my app to clean out any extracted files from a temp location.
It seems I cannot find anyway to completely delete directories and files that I know I created by extracting them from a compressed file, and that my app has a thread that still has open handles to said files and directories.
Is there anyway I can forcible delete these directories and files, knowing I am responsible for their creation and current handle to them?
here is my code for the external .exe that is decompressing the compressed files, i was not using a "using" block to begin with but changed it and didn't not help.
using (Process proc = new Process())
{
proc.StartInfo.FileName = "cmd";
proc.StartInfo.Arguments = param;
proc.StartInfo.CreateNoWindow = true;
proc.StartInfo.RedirectStandardError = true;
proc.StartInfo.RedirectStandardOutput = true;
proc.StartInfo.RedirectStandardInput = true;
proc.StartInfo.UseShellExecute = false;
proc.Start();
StreamReader reader = proc.StandardOutput;
while (!reader.EndOfStream)
{
lines.Add(reader.ReadLine());
}
reader.Close();
proc.WaitForExit();
}
Upvotes: 0
Views: 179
Reputation: 1294
Without seeing how you are dealing with the decompression, it's not immediately obvious what is happening. However, getting rid of the BackgroundWorker doesn't deal with everything inside of it. Instead, inside of the work being done, you should be properly closing files, releasing handles, etc. The worker thread had the instance, but the application owns the handle. When the thread ends, it's still possible to retain open handles. Make sure things like File.Close()
are being done. If you do this, your application (or anything outside of it) will be able to continue with the delete.
Upvotes: 1