2011-09-13 40 views
1

嗨我创建了多个进程与CreateProcess ,我需要等待他们完成,分析结果。C,创建进程并等待

我不能WaitForSingleObject,因为我需要所有的进程在同一时间运行。

由于每个进程在Process_Information(hProcess) 处有句柄,我可以使用WaitForMultipleObjects,但父进程没有等待孩子结束。 可以使用WaitForMultipleObjects还是有更好的方法?

这是我正在创建的过程:

#define MAX_PROCESS 3 

STARTUPINFO si[MAX_PROCESS]; 
PROCESS_INFORMATION pi[MAX_PROCESS]; 
WIN32_FIND_DATA fileData; 
HANDLE find; 

int j=0, t=0; 

ZeroMemory(&si, sizeof(si)); 


for (t = 0; t < MAX_PROCESS; t++) 
    si[t].cb = sizeof(si[0]); 

ZeroMemory(&pi, sizeof(pi));  

while (FindNextFile(find, &fileData) != 0) 
{ 
    // Start the child process. 
    if (!CreateProcess(_T("C:\\Users\\Kumppler\\Documents\\Visual Studio 2010\\Projects\ \teste3\\Debug\\teste3.exe"), // No module name (use command line) 
         aux2,   // Command line 
         NULL,   // Process handle not inheritable 
         NULL,   // Thread handle not inheritable 
         TRUE,   // Set handle inheritance to FALSE 
         0,    // No creation flags 
         NULL,   // Use parent's environment block 
         NULL,   // Use parent's starting directory 
         &si[j],   // Pointer to STARTUPINFO structure 
         &pi[j])   // Pointer to PROCESS_INFORMATION structure 
     ) 
    { 
     printf("CreateProcess failed (%d).\n", GetLastError()); 
     return; 
    } 
    j++; 

    //find next file related 

}  
FindClose(find);   

WaitForMultipleObjects(MAX_PROCESS, &pi[j].hProcess, FALSE, INFINITE); 
//wait and analyze results 

顺便说一句,我尽量不使用线程。

回答

2

如果您想等待所有的手柄将'bWaitAll'(第三个参数)设置为'TRUE'。

3

WaitForMultipleObjects的预计句柄数组:

HANDLE hanldes[MAX_PROCESS]; 
for (int i = 0; i < MAX_PROCESS; ++i) 
{ 
    handles[i] = pi[i].hProcess; 
} 

WaitForMultipleObjects(MAX_PROCESS, handles, TRUE, INFINITE); 

你也应该知道,对于手柄的WaitForMultipleObjects的最大阵列大小被限制为MAXIMUM_WAIT_OBJECTS(它是64)。

+0

谢谢,解决了这个问题 – Caio