2

I have the code below:

proc.Start(); // "proc" opens "foo.txt" and reads it.
proc.Kill();
proc.WaitForExit();
cleanup(); // deletes "foo.txt"

I get an exception telling me "foo.txt" is being used by another process. The only process that would possibly use "foo.txt" is "proc", which I've killed and waited for its exit.

Does Process.Kill not release resources of the killed process? If so, how do I make release the resources held by the killed process so I may clean up.

Thank you.

igbgotiz
  • 892
  • 1
  • 8
  • 28
  • `Process.Kill` does nothing except signal a process for termination. When a process terminates that is when its handles are relased. I think three are some edge cases where a program can "exit" and `WaitForExit` can return but the program has not "terminated" yet and still has some of its handles. – Scott Chamberlain Nov 22 '14 at 05:07
  • Can also try GC.Collect before cleanup() and see what happens. – Alexandru Nov 22 '14 at 05:08
  • @ScottChamberlain as per MSDN Kill does terminate the process. While CloseMainWindow signals the process for termination. Check out [Process.Kill MSDN](http://msdn.microsoft.com/en-us/library/system.diagnostics.process.kill%28v=vs.110%29.aspx) – Piyush Parashar Nov 22 '14 at 05:40
  • Is that txt file created by you? Or created by the process you killed? You could try to loop (with a wait in it) to check if file is locked: http://stackoverflow.com/a/11060322/578411 – rene Nov 22 '14 at 10:48

1 Answers1

-3

You can do this:

do
{
    //wait for process to exit

}while (!myProcess.WaitForExit(1000)); //milliseconds to wait for exit

cleanup();

Also take a look here WaitForExit MSDN

Piyush Parashar
  • 866
  • 8
  • 20