I am starting a child process which should end within a defined timeout period along the following lines:
using (Process process = process.Start(startInfo))
{
if (!process.WaitForExit(timeOutMilliSeconds))
{
if (!process.HasExited())
{
process.Kill();
}
}
}
The child process is a CPU-intense calculation engine (a SAT solver). My main program is written in C#
and developed using Visual Studio 2013 and Windows 7 64-bit.
Most of the time, the above code works and a proper process timeout termination is accomplished. But in some 5% of the cases, process.Kill()
has no visible effect. The child process remains active.
I have tried to start the child process with a lower priority. I also tried to suspend it before killing it. Calling TASKKILL /F
as external tool also did not always help. I noticed, the problem does not occur for all SAT solvers. I have administrative rights but do not run the main program as Administrator.
What could I do to reliably terminate the child process of my application?
Edited:
Apart from exceeding their timeout budget, the child processes behave quite normal and can be killed manually using Process Explorer (without Administrator elevation). The question is how to make sure, that no process is left running after timeout.
Workaround:
I have implemented a timeout tool using VC++ 2013. Rather than calling the child process directly, I now call my tool which in turn calls the child process. The tool is using CreateProcess() and TerminateProcess() to start and stop the child process.
It is probably possible to call CreateProcess() and TerminateProcess() from the C#
program directly without spawning an additional process.
The workaround is not elegant, but it does solve my problem.