Code:
'1) Why does ending main thread close message boxes in other threads? '2) What is wrong with this WaitForMultipleObjectsEx statement? It doesn't wait. '3) Does ending main thread without closing MSGBOX's in other threads cause a leak? '4) WaitForSingleObject working fine. #COMPILE EXE #DIM ALL #INCLUDE "WIN32API.INC" FUNCTION PBMAIN() LOCAL MaximumThread AS DWORD, result AS LONG, x AS LONG, ThreadsWanted AS LONG, counter AS LONG ThreadsWanted = 2 MaximumThread = 10 REDIM hThread(MaximumThread) AS DWORD 'Create 2 threads and place handle in hThread() FOR counter = 1 TO ThreadsWanted FOR x = 1 TO MaximumThread IF hThread(x) = 0 THEN THREAD CREATE MyThread(x) TO hThread(x) SLEEP 1000 EXIT FOR END IF NEXT NEXT 'This works with single objects: 'FOR x = 1 to MaximumThread ' IF hThread(x) then ' WaitForSingleObject hThread(x), %INFINITE ' END IF 'NEXT 'The correction is to change hThread(0) to hThread(1) though the array starts with element 0 WaitForMultipleObjectsEx MaximumThread, VARPTR(hThread(0)), %TRUE, %INFINITE, %FALSE LOCAL zBuffer AS ASCIIZ * %MAX_PATH FormatMessage %FORMAT_MESSAGE_FROM_SYSTEM, BYVAL %NULL, Result, %NULL, zBuffer, SIZEOF(zBuffer), BYVAL %NULL MSGBOX zBuffer + "End me first. Why didn't I wait?" 'Close all handles. FOR x = 1 TO MaximumThread IF hThread(x) THEN THREAD CLOSE hThread(x) TO result hThread(x) = 0 END IF NEXT END FUNCTION FUNCTION MyThread (BYVAL x AS LONG) AS LONG MSGBOX "MyThread(" + FORMAT$(x) + ") running" END FUNCTION
Comment