0

I am running an application in a loop using Process.Start sometimes that application gets stuck (can't modify it) and I shut it down with .Close() if it hasn't update. However, the application creates some files that it locks and those are not released on .Close(). They don't get released until I close my application and cause an error when I try to run again. How can I close those files, so I can continue and run the application again.

bool done = false;
ProcessStartInfo extApp = new ProcessStartInfo();
extApp.Arguments = (dataDir + "cfast.in");
extApp.FileName = cfastPath;
extApp.UseShellExecute = false;

using (Process proc = Process.Start(extApp))
{
  bool stuck = false;
  DateTime lastMod = DateTime.Now;
  string resPath = dataDir + @"cfast.out";
  while (!done && !stuck)
  {
    done = proc.WaitForExit(5000);
    if (File.Exists(resPath))
    {
      lastMod = File.GetLastWriteTime(resPath);
      if ((DateTime.Now - lastMod) > TimeSpan.FromSeconds(10))
        stuck = true;
    }
  }
  if (done)
  {
    exitCode = proc.ExitCode;
  }
  else
  {
    string error = "Warning - Forced closure of CFAST run.";
    logger.Warn(error);
  }
  proc.Close();
}
runfastman
  • 927
  • 2
  • 11
  • 31
  • 1
    `proc.Close()` closes the process handle but does not kill the process; see https://stackoverflow.com/questions/13952635/what-are-the-differences-between-kill-process-and-close-process – Klaus Gütter Feb 16 '22 at 14:56
  • Yes, .Kill() works perfect. Thank, all these years using process.start and I have never even noticed kill. If you want to post as an answer, I will give you credit. – runfastman Feb 16 '22 at 15:12

0 Answers0