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();
}