CreateProcess怎样等待孙线程结束?
MSDN上有段例子代码如下:
if( !CreateProcess( NULL, // No module name (use command line).
strCmdLine.GetBuffer(), // Command line.
NULL, // Process handle not inheritable.
NULL, // Thread handle not inheritable.
FALSE, // Set handle inheritance to FALSE.
0, // No creation flags.
NULL, // Use parent's environment block.
NULL, // Use parent's starting directory.
&si, // Pointer to STARTUPINFO structure.
&pi ) // Pointer to PROCESS_INFORMATION structure.
)
{
ErrorExit( "CreateProcess failed." );
}
// Wait until child process exits.
DWORD dRet = WaitForSingleObject( pi.hProcess, INFINITE );
// Close process and thread handles.
CloseHandle( pi.hThread );
CloseHandle( pi.hProcess );
我有2个疑问:
1,我看了下CreateProcess的解释,是说如果等待由CreateProcess启动子线程结束,那么需要使用WaitForInputIdle,但是这里如果换成WaitForInputIdle的话,是没法等待子线程结束的,必须用WaitForSingleObject,我想知道为什么用WaitForInputIdle会不成功,以及他俩的区别;
2,如果由CreateProcess产生的子线程又启动了一个新的线程,相当于这个程序的孙线程,开启孙线程后,子线程就结束了,孙线程继续运行,过一段时间孙线程才结束。我想问下,通过什么方法可以等待孙线程结束?甚至孙线程的子线程结束。也就是说,如何使用类似WaitForSingleObject的方法,等待子线程以及所有由子线程生成的线程全部结束后,才返回?
谢谢!