<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[WaitForMultipleObjects funktioniert nicht richtig??]]></title><description><![CDATA[<p>Hallo,<br />
ich habe folgendes Problem:<br />
Ich habe 3 Prozesse mit CreateProcess erzeugt (die z.b. calc, mspaint und cmd ausführen).<br />
Dazu habe ich mir die Handles aller erzeugten Prozesse in einem HANDLE array (hnd) gespeichert.</p>
<p>Nun woll ich mit Hilfe von res = WaitForMultipleObjects(3, hnd, FALSE,INFINITE) solange warten bis ein Prozess beendet wird, wobei laut doku res den index des Arrays zurück gibt...</p>
<p>Das ganze passiert in eine Schleife. Das &quot;INFINITE&quot; bedeutet ja, das er solange wartet bis ein Prozess beendet wurde...da dies in einer Schleife passiert müsste er doch nachdem ein Prozess beendet wurde wieder auf den nächsten warten..<br />
Das passiert aber nicht. Er warten am Anfang und sobald ich ein Prozess beende, zeigt er mir sogar den richtigen Index an, aber er wartet nicht auf den nächsten...Also er kehrt sofort aus der Funktion wieder zurück...</p>
<p>Warum??</p>
<p>Hier is der Code:</p>
<pre><code class="language-cpp">STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo;

HANDLE hnd[3];

ZeroMemory( &amp;sInfo, sizeof(sInfo) );
sInfo.cb = sizeof( sInfo );
ZeroMemory( &amp;pInfo, sizeof(pInfo) ); 

if( argc &lt; 2 )
{
     printf(&quot;Aufruf: %s [Programm1, Programm2, ...]\n&quot;, argv[0]);
     return -1;
}

wchar_t wszTemp[100];

for (int i = 1; i&lt; argc; i++)
{
     MultiByteToWideChar(CP_ACP, 0, argv[i], -1, wszTemp, sizeof(wszTemp) / sizeof(wchar_t));
     if( !CreateProcess( NULL, wszTemp, NULL, NULL, FALSE, 0, NULL, NULL, &amp;sInfo, &amp;pInfo ) )
     { 
         printf( &quot;\nFehler&quot;);
     }
     else
     {
         hnd[i] = pInfo.hProcess;
     }
}

DWORD dwResult;
while(1)
{
    dwResult = WaitForMultipleObjects(3, hnd, FALSE,INFINITE);
    printf(&quot;Prozess mit Index %d wurde beendet\n&quot;,dwResult);

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/240222/waitformultipleobjects-funktioniert-nicht-richtig</link><generator>RSS for Node</generator><lastBuildDate>Mon, 06 Apr 2026 10:05:57 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/240222.rss" rel="self" type="application/rss+xml"/><pubDate>Sun, 03 May 2009 19:22:03 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to WaitForMultipleObjects funktioniert nicht richtig?? on Sun, 03 May 2009 19:22:03 GMT]]></title><description><![CDATA[<p>Hallo,<br />
ich habe folgendes Problem:<br />
Ich habe 3 Prozesse mit CreateProcess erzeugt (die z.b. calc, mspaint und cmd ausführen).<br />
Dazu habe ich mir die Handles aller erzeugten Prozesse in einem HANDLE array (hnd) gespeichert.</p>
<p>Nun woll ich mit Hilfe von res = WaitForMultipleObjects(3, hnd, FALSE,INFINITE) solange warten bis ein Prozess beendet wird, wobei laut doku res den index des Arrays zurück gibt...</p>
<p>Das ganze passiert in eine Schleife. Das &quot;INFINITE&quot; bedeutet ja, das er solange wartet bis ein Prozess beendet wurde...da dies in einer Schleife passiert müsste er doch nachdem ein Prozess beendet wurde wieder auf den nächsten warten..<br />
Das passiert aber nicht. Er warten am Anfang und sobald ich ein Prozess beende, zeigt er mir sogar den richtigen Index an, aber er wartet nicht auf den nächsten...Also er kehrt sofort aus der Funktion wieder zurück...</p>
<p>Warum??</p>
<p>Hier is der Code:</p>
<pre><code class="language-cpp">STARTUPINFO sInfo;
PROCESS_INFORMATION pInfo;

HANDLE hnd[3];

ZeroMemory( &amp;sInfo, sizeof(sInfo) );
sInfo.cb = sizeof( sInfo );
ZeroMemory( &amp;pInfo, sizeof(pInfo) ); 

if( argc &lt; 2 )
{
     printf(&quot;Aufruf: %s [Programm1, Programm2, ...]\n&quot;, argv[0]);
     return -1;
}

wchar_t wszTemp[100];

for (int i = 1; i&lt; argc; i++)
{
     MultiByteToWideChar(CP_ACP, 0, argv[i], -1, wszTemp, sizeof(wszTemp) / sizeof(wchar_t));
     if( !CreateProcess( NULL, wszTemp, NULL, NULL, FALSE, 0, NULL, NULL, &amp;sInfo, &amp;pInfo ) )
     { 
         printf( &quot;\nFehler&quot;);
     }
     else
     {
         hnd[i] = pInfo.hProcess;
     }
}

DWORD dwResult;
while(1)
{
    dwResult = WaitForMultipleObjects(3, hnd, FALSE,INFINITE);
    printf(&quot;Prozess mit Index %d wurde beendet\n&quot;,dwResult);

}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1704966</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1704966</guid><dc:creator><![CDATA[Feldmaus001]]></dc:creator><pubDate>Sun, 03 May 2009 19:22:03 GMT</pubDate></item><item><title><![CDATA[Reply to WaitForMultipleObjects funktioniert nicht richtig?? on Sun, 03 May 2009 19:51:09 GMT]]></title><description><![CDATA[<p>Ist ein Handle erstmal signalisiert, bleibt es signalisiert. D.h. ein erneutes WaitForMultipleObjects(..) bringt wieder dasselbe signalisierte Handle.</p>
<p>Entferne das signalisierte Handle aus dem Array, dann wird auf die Beednigung des nächsten Prozesses gewartet.</p>
<p>Simon</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1704974</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1704974</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Sun, 03 May 2009 19:51:09 GMT</pubDate></item><item><title><![CDATA[Reply to WaitForMultipleObjects funktioniert nicht richtig?? on Sun, 03 May 2009 19:58:17 GMT]]></title><description><![CDATA[<p>das habe ich schon gemacht,<br />
ohne erfolg...<br />
oder ich hab was falsch entfernt..</p>
<p>wie mach ich das am besten?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1704981</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1704981</guid><dc:creator><![CDATA[Feldmaus001]]></dc:creator><pubDate>Sun, 03 May 2009 19:58:17 GMT</pubDate></item><item><title><![CDATA[Reply to WaitForMultipleObjects funktioniert nicht richtig?? on Sun, 03 May 2009 20:01:26 GMT]]></title><description><![CDATA[<p>Zeig doch mal wie du das gemacht hast...</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1704985</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1704985</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Sun, 03 May 2009 20:01:26 GMT</pubDate></item><item><title><![CDATA[Reply to WaitForMultipleObjects funktioniert nicht richtig?? on Sun, 03 May 2009 20:06:25 GMT]]></title><description><![CDATA[<p>ich habe das erstmal testweise so gemacht, das ich mir ein zweites array erzeugt habe, mit einem element weniger, also das 3. Handle fehlt:</p>
<pre><code class="language-cpp">HANDLE hnd2[2];
memcpy(hnd2,hnd,2);

dwResult = WaitForMultipleObjects(3, hnd, FALSE,INFINITE);
printf(&quot;Prozess mit Index %d wurde beendet\n&quot;,dwResult);

dwResult = WaitForMultipleObjects(2, hnd2, FALSE,INFINITE);
printf(&quot;Prozess mit Index %d wurde beendet\n&quot;,dwResult);
</code></pre>
<p>ich hab dann den 3. Prozess zuerst beendet, es ist dann also nicht mehr in hnd2 drin....aber beim zweiten wait wartet er trotzdem nich..</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1704992</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1704992</guid><dc:creator><![CDATA[Feldmaus001]]></dc:creator><pubDate>Sun, 03 May 2009 20:06:25 GMT</pubDate></item><item><title><![CDATA[Reply to WaitForMultipleObjects funktioniert nicht richtig?? on Sun, 03 May 2009 20:28:25 GMT]]></title><description><![CDATA[<p>Hallo Feldmaus,</p>
<pre><code class="language-cpp">int handlenum = 0;

for (int i = 1; i&lt; argc; i++)
{
	...
	if( !CreateProcess...
	{...}
	else
	{
		hnd[handlenum++] = pInfo.hProcess;
		CloseHandle(pInfo.hThread); // vergessen?
	}
}

while (handlenum)
{
	dwResult = WaitForMultipleObjects(handlenum, hnd, FALSE, INFINITE);
	printf(&quot;Prozess mit Index %d wurde beendet\n&quot;, dwResult);
	CloseHandle(hnd[dwResult]);
	hnd[dwResult] = hnd[--handlenum]; // handle[terminated] = handle[last]
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1705006</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705006</guid><dc:creator><![CDATA[sapero]]></dc:creator><pubDate>Sun, 03 May 2009 20:28:25 GMT</pubDate></item><item><title><![CDATA[Reply to WaitForMultipleObjects funktioniert nicht richtig?? on Sun, 03 May 2009 20:28:58 GMT]]></title><description><![CDATA[<p>Hier ein Bsp.<br />
Anstelle von Prozess Handles habe ich hier Thread Handles genommen. Spielt aber für die Logik keine Rolle.</p>
<pre><code class="language-cpp">DWORD WINAPI threadProc(LPVOID /*arg*/)
{
	Sleep(2000);
	return 0;
}

int main()
{
	HANDLE hHandles[4];
	hHandles[0] = CreateThread(0, 0, &amp;threadProc, 0, 0, 0);
	hHandles[1] = CreateThread(0, 0, &amp;threadProc, 0, 0, 0);
	hHandles[2] = CreateThread(0, 0, &amp;threadProc, 0, 0, 0);
	hHandles[3] = CreateThread(0, 0, &amp;threadProc, 0, 0, 0);
	DWORD nCount = 4;

	while (nCount &gt; 0)
	{
		DWORD dwResult = WaitForMultipleObjects(nCount, hHandles, FALSE, INFINITE);
		DWORD index = dwResult - WAIT_OBJECT_0;

		CloseHandle(hHandles[index]);
		hHandles[index] = 0;

		for (DWORD i = index; i &lt; nCount; ++i)
		{
			if (i &lt; nCount - 1)
			{
				hHandles[i] = hHandles[i + 1];
			}
			else
			{
				hHandles[i] = 0;
			}
		}
		--nCount;
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1705008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705008</guid><dc:creator><![CDATA[theta]]></dc:creator><pubDate>Sun, 03 May 2009 20:28:58 GMT</pubDate></item><item><title><![CDATA[Reply to WaitForMultipleObjects funktioniert nicht richtig?? on Sun, 03 May 2009 21:06:51 GMT]]></title><description><![CDATA[<p>Danke euch beiden,<br />
hat geklappt <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1705029</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1705029</guid><dc:creator><![CDATA[Feldmaus001]]></dc:creator><pubDate>Sun, 03 May 2009 21:06:51 GMT</pubDate></item></channel></rss>