Announcement

Collapse
No announcement yet.

WaitForMultipleObjects not waiting and more

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • WaitForMultipleObjects not waiting and more

    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
    Last edited by Mike Doty; 21 Dec 2007, 04:28 PM.
    The world is full of apathy, but who cares?

  • #2
    Code:
    'Element 0 changed to 1 though the array starts with element 0.
    'I guess the first element must be non-zero or it just exits?
    WaitForMultipleObjects BYVAL MaximumThread , BYVAL VARPTR(hThread(1)), %TRUE, %INFINITE
    The world is full of apathy, but who cares?

    Comment


    • #3
      Mike,

      Your "FOR x...NEXT" loop starts with a count value of one (1), not zero (0). WFMO will immediately fail if any of the passed object handles are invalid.

      Comment


      • #4
        Yes, that was it.
        I should have passed element 1 instead of 0 in array.
        The world is full of apathy, but who cares?

        Comment

        Working...
        X