Announcement

Collapse
No announcement yet.

Thread programming Problem

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

  • Thread programming Problem

    Hi
    I am new in programming with Powerbasic and i have a little problem
    with Thread programming.
    the command:
    THREAD CREATE MyThreadFunction(1) TO hThread???
    returns me in hThread always a 0 but the Thread is runing.
    So that i not can not suspend,resume or change Priority of the thread
    Can anybody tell me what i make wrong in the following code.
    For help would be I most grateful.
    With kind regards
    Andreas
    Code:
    
    
    Code:
    [SIZE=2]#COMPILE EXE[/SIZE]
    [SIZE=2]#DIM ALL[/SIZE]
    [SIZE=2]#INCLUDE "Win32API.inc"[/SIZE]
    [SIZE=2]%IDOK = 1[/SIZE]
    [SIZE=2]%IDCANCEL = 2[/SIZE]
    [SIZE=2]%IDTEXT = 100[/SIZE]
    [SIZE=2]%BN_CLICKED = 0[/SIZE]
    [SIZE=2]%BS_DEFAULT = 1[/SIZE]
    [SIZE=2]%MF_ENABLED = 0[/SIZE]
    [SIZE=2]%WM_COMMAND = &H111[/SIZE]
    [SIZE=2]%ID_OPEN = 401[/SIZE]
    [SIZE=2]%ID_EXIT = 402[/SIZE]
    [SIZE=2]%ID_OPTION1 = 403[/SIZE]
    [SIZE=2]%ID_OPTION2 = 404[/SIZE]
    [SIZE=2]%ID_HELP = 405[/SIZE]
    [SIZE=2]%ID_ABOUT = 406[/SIZE]
     
    [SIZE=2]FUNCTION PBMAIN () AS LONG[/SIZE]
    [SIZE=2]GLOBAL HWindow AS LONG[/SIZE]
    [SIZE=2]GLOBAL lResult AS LONG[/SIZE]
    [SIZE=2]GLOBAL hThread AS DWORD[/SIZE]
    [SIZE=2]DIALOG NEW 0, "Thread Handle Test", ,, 350, 250, %WS_SYSMENU OR %WS_CAPTION TO HWindow[/SIZE]
    [SIZE=2]CONTROL ADD STATUSBAR, HWindow, 500, "",0,0,0,0,%CCS_TOP[/SIZE]
    [SIZE=2]STATUSBAR SET PARTS HWindow,500, 150,150,99999[/SIZE]
    [SIZE=2]'--------------------------------------------------------------------------[/SIZE]
    [SIZE=2]THREAD CREATE MyThreadFunction(1) TO hThread???[/SIZE]
    [SIZE=2]STATUSBAR SET TEXT HWindow, 500, 2, 0, "Thread Handle : " + STR$(hThread)[/SIZE]
    [SIZE=2]'--------------------------------------------------------------------------[/SIZE]
    [SIZE=2]DIALOG SHOW MODAL HWindow , CALL CallBackProc TO lResult[/SIZE]
    [SIZE=2]END FUNCTION[/SIZE]
    [SIZE=2]FUNCTION MyThreadFunction(BYVAL x AS LONG) AS LONG[/SIZE]
    [SIZE=2]LOCAL tick AS DWORD[/SIZE]
    [SIZE=2]LOCAL a AS DWORD[/SIZE]
    [SIZE=2]DO UNTIL a = 1000000000[/SIZE]
    [SIZE=2]a = a + 1[/SIZE]
    [SIZE=2]SLEEP 1[/SIZE]
    [SIZE=2]STATUSBAR SET TEXT HWindow, 500, 1, 0, "Thread Output : " + STR$(a)[/SIZE]
    [SIZE=2]LOOP[/SIZE]
    [SIZE=2]END FUNCTION[/SIZE]
    [SIZE=2]CALLBACK FUNCTION CallBackProc()[/SIZE]
    [SIZE=2]IF CB.MSG = %WM_COMMAND THEN[/SIZE]
    [SIZE=2]IF CB.CTL => %ID_OPEN AND CB.CTL <= %ID_ABOUT THEN[/SIZE]
    [SIZE=2]FUNCTION = 1[/SIZE]
    [SIZE=2]END IF[/SIZE]
    [SIZE=2]END IF[/SIZE]
    [SIZE=2]END FUNCTION[/SIZE]
    Last edited by Andreas Gaida; 21 Apr 2009, 07:42 AM.

  • #2
    Please put code within (CODE) (/CODE) brackets.
    This works, I'll look at your code some more.

    Code:
    #COMPILE EXE
    #DIM ALL
    %ID_OPEN = 401
    %ID_ABOUT = 406
    GLOBAL ghWindow AS LONG
    GLOBAL glResult AS LONG
    GLOBAL ghThread AS DWORD
    FUNCTION PBMAIN () AS LONG
      DIALOG NEW 0, "Thread Handle Test", ,, 350, 250, %WS_SYSMENU OR %WS_CAPTION TO ghWindow
      CONTROL ADD STATUSBAR, ghWindow, 500, "",0,0,0,0,%CCS_TOP
      STATUSBAR SET PARTS ghWindow,500, 150,150,99999
      '--------------------------------------------------------------------------
      THREAD CREATE MyThreadFunction(1) TO ghThread???
      STATUSBAR SET TEXT ghWindow, 500, 2, 0, "Thread Handle : " + STR$(ghThread)
      '--------------------------------------------------------------------------
      DIALOG SHOW MODAL ghWindow , CALL CallBackProc TO glResult
      'THREAD Close ghThread to ghThread
    END FUNCTION
    FUNCTION MyThreadFunction(BYVAL x AS LONG) AS LONG
      LOCAL a AS DWORD
      DO UNTIL a = 1000000000
        INCR a
        SLEEP 1
        STATUSBAR SET TEXT ghWindow, 500, 1, 0, "Thread Output : " + STR$(a)
      LOOP
    END FUNCTION
    CALLBACK FUNCTION CallBackProc()
      IF CB.MSG = %WM_COMMAND THEN
        IF CB.CTL => %ID_OPEN AND CB.CTL <= %ID_ABOUT THEN
          FUNCTION = 1
        END IF
      END IF
    END FUNCTION

    Comment


    • #3
      Your code works here with hThread??? = 104.
      PB samples use SLEEP 50 after THREAD CREATE.

      Comment


      • #4
        You should never need a sleep. (except to replace "ready" event when starting lots of TOEs rapidly)

        How to do worker thread with a GUI...heavily commented :
        GUI + Worker Thread + Abort Demo 11-24-07

        MCM
        Michael Mattias
        Tal Systems (retired)
        Port Washington WI USA
        [email protected]
        http://www.talsystems.com

        Comment


        • #5
          Don't assume thread has been created without the sleep.
          This example shows threads still being created.
          Code:
          GLOBAL gCounter AS DWORD
          FUNCTION PBMAIN () AS LONG
            FOR x& = 1 TO 100
              THREAD CREATE MyThreadFunction(1) TO hThread???
              IF hThread??? = 0 THEN ? "ERROR"
            NEXT
            ? STR$(gCounter) 'threads still being created
            ? STR$(gCounter) '100
          END FUNCTION
          FUNCTION MyThreadFunction(BYVAL x AS LONG) AS LONG
             INCR gCounter
          END FUNCTION
          This works a little better.
          Code:
          GLOBAL gCounter AS DWORD
          FUNCTION PBMAIN () AS LONG
            FOR x& = 1 TO 100
              THREAD CREATE MyThreadFunction(1) TO hThread???
              SLEEP 1 'give time to allocate
              IF hThread??? = 0 THEN ? "ERROR"
            NEXT
            ? STR$(gCounter) 'threads created
            ? STR$(gCounter) '100
          END FUNCTION
          FUNCTION MyThreadFunction(BYVAL x AS LONG) AS LONG
             INCR gCounter
          END FUNCTION
          Last edited by Mike Doty; 21 Apr 2009, 02:57 AM.

          Comment


          • #6
            Code:
            GLOBAL gCounter AS DWORD
            FUNCTION PBMAIN () AS LONG
              THREAD CREATE MyThreadFunction(1) TO hThread???
              ? STR$(gCounter) '0
            END FUNCTION
            FUNCTION MyThreadFunction(BYVAL x AS LONG) AS LONG
               INCR gCounter
            END FUNCTION

            Comment


            • #7
              Hi i thank you for the help.
              But is very mysterious thing i test the same
              programm .exe of your code Mike (Yesterday 11:39 PM) on my main Computer and the Handle is 0 and on my Laptop , there is the Handle of the Thread 96.
              I will install my windows xp new and test it again.

              With kind regards
              Andreas

              Comment


              • #8
                If SLEEP 50 or more is added after the THREAD CREATE you still get 0?
                Reinstalling the operating system seems a bit extreme.
                Code:
                 
                GLOBAL gCounter AS DWORD
                FUNCTION PBMAIN () AS LONG
                 THREAD CREATE MyThreadFunction(1) TO hThread???
                 SLEEP 5000
                 ? STR$(gCounter) '1
                END FUNCTION
                FUNCTION MyThreadFunction(BYVAL x AS LONG) AS LONG
                 INCR gCounter
                END FUNCTION
                Last edited by Mike Doty; 21 Apr 2009, 02:05 AM.

                Comment


                • #9
                  yes all your code make me a 0 Handle after sleep 5000 too.
                  but only on my main Computer on the laptop works fine.
                  Main Computer : Athlon X2 4800

                  Comment


                  • #10
                    Wow, last example is just a counter and should return 1.
                    I suggest sending a link to this thread to [email protected]

                    Comment


                    • #11
                      the last code with the encounter was ok but i testet the handle and is was in your code 0 too the threads are always runing but i not became a handle back

                      Comment


                      • #12
                        Does this give you the handles?
                        Code:
                        FUNCTION PBMAIN () AS LONG
                         LOCAL counter, hTHREAD AS LONG
                         FOR counter = 1 TO 2
                           THREAD CREATE MyThreadFunction(Counter) TO hThread
                           SLEEP 50
                           ? "The handle is" + STR$(hThread)
                           REM THREAD CLOSE hTHREAD to hThread
                         NEXT
                         ? "Done"
                        END FUNCTION
                        FUNCTION MyThreadFunction(BYVAL hello AS LONG) AS LONG
                        END FUNCTION
                        Code:
                        FUNCTION PBMAIN () AS LONG
                         LOCAL counter, hTHREAD AS LONG, s AS STRING
                         FOR counter = 1 TO 10
                           THREAD CREATE MyThreadFunction(Counter) TO hThread
                           SLEEP 50
                           s = s +  "The handle is" + STR$(hThread) + $CRLF
                           REM THREAD CLOSE hTHREAD to hThread
                         NEXT
                         ? s
                        END FUNCTION
                        FUNCTION MyThreadFunction(BYVAL hello AS LONG) AS LONG
                        END FUNCTION
                        By the way.
                        Your posted code isn't within a code window because the CODE and /CODE statements must be within brackets []
                        Last edited by Mike Doty; 21 Apr 2009, 03:20 AM.

                        Comment


                        • #13
                          Hi best thanks for your help.
                          I have restore my windows xp to a very clean version and now
                          runing all your and my codes .Thanks for the info about code /code .

                          With kind regards
                          Andreas
                          Last edited by Andreas Gaida; 21 Apr 2009, 07:44 AM.

                          Comment


                          • #14
                            An "EXTREME" such as a computer system reinstall fixes the problem

                            Should NEVER be necessary (unless you had other problems that could confuse results) although it was a proud and stupid following of "Oh it still doesn't work???.....Reinstall the system and call me back" train of thought

                            Danged near impossible to diagnose without being there to diagnose, and somehow the results are a cure (or as it appears) so most would stop diagnosing.

                            Is it time to put the tin-foil hat back on??? (I think the MLB satellite should be flying over soon for brain scans )

                            (If you missed that last comment, you need to watch "The Simpsons" a bit more)
                            Engineer's Motto: If it aint broke take it apart and fix it

                            "If at 1st you don't succeed... call it version 1.0"

                            "Half of Programming is coding"....."The other 90% is DEBUGGING"

                            "Document my code????" .... "WHYYY??? do you think they call it CODE? "

                            Comment

                            Working...
                            X