I have a pair of Windows console programs, parent written in C#, child written in C; the parent calls the child with a filename as argument, gets output, waits for exit, calls again with the next in a list of data files and so on. Going through the entire list of data files could take many hours, so in testing I usually want to kill the parent process with ^C, figuring if any child process is still running, if it doesn't die because of that, it should be killed a few seconds later by the timer it sets.
What actually happens is the parent process exits, I get the console back, but the child process is still there in the background, using no CPU, hung permanently until killed with task manager. Why is that happening, and how can it be prevented?
Parent process calling code:
var worker = new Process()
{
StartInfo = new ProcessStartInfo("/ayane/ayane.exe", "-t=10 " + filename)
{
RedirectStandardError = true,
RedirectStandardOutput = true,
UseShellExecute = false,
}
};
worker.Start();
var output = worker.StandardOutput.ReadToEnd();
worker.WaitForExit();
Child code that sets a timer to kill the process if it hasn't finished in a few seconds:
static VOID CALLBACK timeout(PVOID a, BOOLEAN b) { ExitProcess(0); }
...
HANDLE timer = 0;
CreateTimerQueueTimer(&timer, 0, timeout, 0, (DWORD)(time_limit * 1000),
0, 0);
Update: I tried a test just now where I took out the RedirectStandardOutput
and RedirectStandardError
so presumably it's not a pipe situation any more, and the problem still happens exactly the same way, so it doesn't seem to be to do with redirection and broken pipes after all.