Announcement

Collapse
No announcement yet.

Need a simple prog to run a thread function

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

    Need a simple prog to run a thread function

    I'm looking for a simple or easy to understand prog to run a separate process in a thread function. Anyone can spare some codes?

    #2
    You could start with this 'thread'

    Comment


      #3
      or this

      Comment


        #4
        This is about as simple and easy as I can come up with to demonstrate a thread doing something while the main application continues to do things:
        '
        Code:
        #COMPILE EXE
        #DIM ALL
        #DEBUG ERROR ON
        #DEBUG DISPLAY ON
        
        THREAD FUNCTION T1(BYVAL x AS LONG) AS LONG
        LOCAL i AS LONG
           FOR i = 1 TO 3
               INCR x
               TXT.PRINT "Thread x = " & STR$(x)
               SLEEP 1000
           NEXT
        END FUNCTION
        
        FUNCTION PBMAIN() AS LONG
            LOCAL lDebug AS LONG: TXT.WINDOW EXE.FULL$, 10,10,45,85 TO lDebug
            LOCAL x, hThread AS LONG
            TXT.PRINT "MAIN THREAD"
            THREAD CREATE T1(0) TO hThread
            DO
                INCR x
                TXT.PRINT "Main application x = " & STR$(x)
                SLEEP 300
            LOOP UNTIL THREADCOUNT = 1
            TXT.PRINT "Thread has ended"
        
        'Finalise
            TXT.COLOR = %RGB_BLUE
            TXT.PRINT
            TXT.PRINT "  ....Press any key to exit": TXT.WAITKEY$: TXT.END
        END FUNCTION
        '
        ?​
        =========================
        https://camcopng.com
        =========================

        Comment


          #5
          Code:
          FUNCTION PBMAIN() AS LONG
           LOCAL h AS LONG
           THREAD CREATE background(0) TO h
           THREAD CLOSE h TO h
           DO
            SLEEP 50
           LOOP UNTIL THREADCOUNT = 1
           BEEP
          END FUNCTION
          
          THREAD FUNCTION background(BYVAL NotNeeded AS LONG) AS LONG
           ? "Click to finish",,FUNCNAME$
          END FUNCTION

          Comment


            #6
            Thanks All

            Comment


              #7
              How do I check the status of a thread function, check whether it has been completed or not ?

              Comment


                #8
                Originally posted by Mannish Bhandari View Post
                How do I check the status of a thread function, check whether it has been completed or not ?

                THREAD STATUS statement
                THREAD CLOSE statement
                THREADCOUNT function

                ​Alos see post #4:
                LOOP UNTIL THREADCOUNT = 1
                TXT.PRINT "Thread has ended"​

                =========================
                https://camcopng.com
                =========================

                Comment


                  #9
                  How do I check the status of a thread function, check whether it has been completed or not ?
                  Messages can also be sent from threads to controls or use unique globals for each thread.
                  Code:
                  #UNIQUE VAR ON
                  
                  FUNCTION PBMAIN
                   LOCAL hDlg,hThread AS DWORD
                   DIALOG NEW 0, "TEST", , , 180, 30, %WS_OVERLAPPEDWINDOW TO hDlg
                   DIALOG SHOW MODELESS hDlg, CALL MyCallBack
                   THREAD CREATE PrintThread(hDlg) TO hThread
                   THREAD CLOSE  hThread TO hThread
                   DO
                    DIALOG DOEVENTS
                   LOOP WHILE ISWIN(hDlg) OR THREADCOUNT > 1
                  END FUNCTION
                  
                  THREAD FUNCTION PrintThread(BYVAL hDlg AS DWORD) AS LONG
                   LOCAL pagenum AS LONG
                   FOR pagenum = 1 TO 100
                    DIALOG SET TEXT hDlg,USING$("Printed page #",pagenum)
                    IF ISFALSE(ISWIN(hDlg)) THEN BEEP:EXIT FOR
                    SLEEP 50
                   NEXT
                  END FUNCTION
                  
                  CALLBACK FUNCTION MyCallBack()
                   SELECT CASE AS LONG CB.MSG
                    CASE %WM_INITDIALOG
                    CASE %WM_CLOSE
                   END SELECT
                  END FUNCTION

                  Comment


                    #10
                    Thanks Mike and Stuart

                    Comment


                      #11
                      A thread handle is automatically (by Windows) a synchronization object which can be used in a WaitForSingleObject() or [Message]WaitForMultipleObjects() WinAPI function. The handle is signalled ("satisfies the Wait") when the thread function completes.

                      See: https://learn.microsoft.com/en-us/wi...wait-functions.

                      THREAD STATUS is deficient in that fact it can't handle a zero return code from the thread function (means the call failed see PB doc). But you can avoid that issue by THINKING about your exit code BEFORE you write it.

                      The "hThread" value returned by PB THREAD CREATE is in fact a Windows handle, so it may be used with the WinAPI Wait Functions. I have multiple examples of this in the Source Code Forum, e.g., GUI + Worker Thread + Abort Demo 11-24-07 (DDT syntax)
                      Last edited by Michael Mattias; 5 May 2023, 12:06 PM.
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment

                      Working...
                      X
                      😀
                      🥰
                      🤢
                      😎
                      😡
                      👍
                      👎