Announcement

Collapse
No announcement yet.

What is missing that would make this thread work?

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

  • What is missing that would make this thread work?

    I know how to do threads when I have a dialog. How do you do it without a dialog?
    Can this be done with PB Win10? There is a similar sample in the PB CC6 examples.



    Code:
    '-----------------------------------------------------------------------------
     FUNCTION START_COMM_THREAD()AS LONG
    
        LOCAL THREAD_HANDLE     AS LONG
        LOCAL THREAD_RESULT     AS LONG
    
        THREAD CREATE THREADED_COMM_THREAD(0) TO THREAD_HANDLE
        THREAD CLOSE  THREAD_HANDLE TO THREAD_RESULT
    
     END FUNCTION
    '------------------------------------------------------------------------------
     THREAD FUNCTION THREADED_COMM_THREAD(BYVAL NO_VAL AS LONG)AS LONG
      ON ERROR GOTO ERROR_CONTROL
    
      DO
        SLEEP 500
        WINBEEP(300,300)
      LOOP
    
      EXIT FUNCTION
      ERROR_CONTROL:
     END FUNCTION
    '------------------------------------------------------------------------------
    FUNCTION PBMAIN () AS LONG
    
        LOCAL CompletionPort AS DWORD
        LOCAL msg AS TAGMSG
    
        START_COMM_THREAD
    
        DO
         TranslateMessage(msg)
         DispatchMessage(msg)
        LOOP
    
    END FUNCTION
    
    '------------------------------------------------------------------------------

  • #2
    Code:
    global g as long
    
    function pbmain as long
     local h as long
     thread create mythread(0) TO h
     sleep 5000
    
     g = 1
     do:sleep 50:loop until threadcount=1
     thread close h to h
    end function
    
    thread function mythread(byval no_val as long) as long
     do
      sleep 500
      beep
     loop until g
    end function

    Code:
    #INCLUDE "win32api.inc" 'was needed for winbeep and unneeded tagmsg
    '-----------------------------------------------------------------------------
     FUNCTION START_COMM_THREAD()AS LONG
    
        LOCAL THREAD_HANDLE     AS LONG
        LOCAL THREAD_RESULT     AS LONG
    
        THREAD CREATE THREADED_COMM_THREAD(0) TO THREAD_HANDLE
        THREAD CLOSE  THREAD_HANDLE TO THREAD_RESULT
    
     END FUNCTION
    '------------------------------------------------------------------------------
     THREAD FUNCTION THREADED_COMM_THREAD(BYVAL NO_VAL AS LONG)AS LONG
      ON ERROR GOTO ERROR_CONTROL
    
      DO 'endless loop
        SLEEP 500
        WINBEEP(300,300)
        OPEN "junk.dat" FOR INPUT AS #1 'create error to test error handling
      LOOP
    
      EXIT FUNCTION
      ERROR_CONTROL:
      FUNCTION = ERR
    
     END FUNCTION
    '------------------------------------------------------------------------------
    FUNCTION PBMAIN () AS LONG
    
        LOCAL CompletionPort AS DWORD 'not needed
        LOCAL msg AS TAGMSG           'not needed
        START_COMM_THREAD
    
        DO  'didn't need a message pump
         SLEEP 50
        LOOP UNTIL THREADCOUNT=1
    
    END FUNCTION
    
    '------------------------------------------------------------------------------
    Last edited by Mike Doty; 13 Jan 2020, 07:36 AM.
    https://duckduckgo.com instead of google

    Comment


    • #3
      Thanks Mike!

      Comment


      • #4
        Welcome
        This shows how to pass 2 values and how sleep can give thread time to allocate values in this race condition
        Code:
        FUNCTION PBMAIN () AS LONG
         LOCAL qTwoValues AS QUAD, hThread AS LONG
         qTwoValues = MAK(QUAD,1000,2000)
         THREAD CREATE ConnectionMain(VARPTR(qTwoValues)) TO hThread
         SLEEP 1          'race condition
         qTwoValues = 0   'cause problem in thread without above sleep
         THREAD CLOSE hThread TO hThread
         SLEEP 3000 'prevent program from ending for 3-seconds
        END FUNCTION
        
        THREAD FUNCTION ConnectionMain(BYVAL p AS QUAD PTR) AS LONG
         LOCAL value1,value2 AS LONG
         value1 = LO(DWORD, @p) 'put into local variable
         value2 = HI(DWORD, @p) 'put into local variable
         ? USING$("#&#",value1,$CR,value2)
        END FUNCTION
        https://duckduckgo.com instead of google

        Comment

        Working...
        X