开发者

WaitForMultipleObjects failing c++

开发者 https://www.devze.com 2023-03-08 20:10 出处:网络
I am currently writing a program that will run mulitple programs in groups all at once and others on their own.

I am currently writing a program that will run mulitple programs in groups all at once and others on their own.

if( WAIT_FAILED == WaitForMultipleObjects(numberOfProgramsRan, &information[i].hProcess, TRUE, INFINITE) ) { wcerr << L"Failure waiting for process" << endl; }

numberOfProgramsRan is the number of programs 开发者_如何学Cthat i ran in my loop. &information[i] is a vector holding my process information from the create process

When i create the process in a for loop my program will wait if there are two or less processes being created (so two programs being passed in to run) before it runs my next processes. If create more than two processes (or pass in more than two programs in my vector) my WaitForMultipleObjects it fails.

If i need to futher explain my issue please let me know.

Thanks for your help


If you only wait on a single process (index i) you should use WaitForSingleObject. If you're waiting on multiple processes you need to pass in an array of handles as others have said - not a pointer into PROCESS_INFORMATION. If you insist on using WaitFoRmultipleObjects for a single object use:

WaitForMultipleObjects(1, &information[i].hProcess, TRUE, INFINITE)

If you use anything other than 1 then look at the definition of PROCESS_INFORMATION:

typedef struct _PROCESS_INFORMATION {
  HANDLE hProcess;
  HANDLE hThread;
  DWORD  dwProcessId;
  DWORD  dwThreadId;
} PROCESS_INFORMATION, *LPPROCESS_INFORMATION;

The following dwProcessId and dwThreadID will then be incorrectly treated as handles your call will not work as expected.

Something like:

HANDLE hProcess[MAX_PROCESSES];
for(int i=0; i<numberOfProgramsRan; i++)
{
  hProcess[i] = information[i].hProcess;
}
WaitForMultipleObjects(numberOfProgramsRan, hProcess, TRUE, INFINITE);

Will wait on all your processes.


That &information[i].hProcess should be pointer to first element of array of HANDLEs that contains numberOfProgramsRan elements. By your description it does not sound like to be the case, so i don't know how you imagine that it should work.


Make sure you are passing in a pointer to an array of HANDLEs (see the official documentation). I don't know what information[i].hProcess is but it looks like a single HANDLE member contained within a structure/class. Although if it was a single HANDLE I would expect it to only work with 1 process and not 2 as you mention.

Showing the definition of information and how you are initializing/creating the processes here may help as well as reducing the code to a minimum size that still reproduces the issue. Also details on "it fails" may help (error/exception message, what happens, etc...).

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号