Announcement

Collapse

New Sub-Forum

In an effort to help make sure there are appropriate categories for topics of discussion that are happening, there is now a sub-forum for databases and database programming under Special Interest groups. Please direct questions, etc., about this topic to that sub-forum moving forward. Thank you.
See more
See less

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.
    How long is an idea? Write it down.

  • #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
    How long is an idea? Write it down.

    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.
        How long is an idea? Write it down.

        Comment

        Working...
        X