Announcement

Collapse
No announcement yet.

Using multiple timers

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

  • Using multiple timers

    Hi All,
    I need to use at least 3 timers to do a variety of different stuff in a program, I'm wondering if this below method is okay, am I doing correctly ?

    Code:
    ' Multiple Timers.bas
    
    #COMPILE EXE
    #DIM ALL
    
        #INCLUDE "Win32Api.inc"
    
     %IDC_ATimer = 101
     %IDC_BTimer = 102
     %IDC_CTimer = 103
     %IDC_GDisplay  = 105
    
    ' handles for timers
     GLOBAL hAtim  , hBtim , hCtim AS DWORD
    
    
    
    FUNCTION PBMAIN () AS LONG
    
      LOCAL hSpin AS DWORD
       DIALOG NEW PIXELS, 0 , "My Dialog", 100 ,50 , 300, 300,_
                %WS_SYSMENU, %WS_EX_TRANSPARENT TO hSpin
    
        CONTROL ADD GRAPHIC, hSpin, %IDC_GDisplay, "", _
                 10, 10, 150, 150
    
    
    
         DIALOG SHOW MODAL  hSpin, CALL DlgSpProc()
    
    
    
    END FUNCTION
    
    
    
    
    
    
    
       CALLBACK FUNCTION DlgSpProc() AS LONG
    
    
        SELECT CASE CB.MSG
            CASE %WM_INITDIALOG
                ' setup the timers
                  hAtim = settimer(CB.HNDL, %IDC_ATimer, 15, 0)
                  hBtim = settimer(CB.HNDL, %IDC_BTimer, 150, 0)
                  hCtim = settimer(CB.HNDL, %IDC_CTimer, 200, 0)
    
    
    
          CASE %WM_TIMER
                IF hAtim THEN
                    ' Do stuff A  --- this is a ONE time process  -- after this we kill the associated timer
                    killtimer(CB.HNDL, %IDC_ATimer)
                    hATim = 0​
                END IF
    
                IF hBtim THEN
                    ' Do stuff B
                END IF
                IF hCtim THEN
                    ' Do stuff C
                END IF
    
    
    
    
          CASE %WM_DESTROY
                ' clean up the timers
                 IF hATim THEN
                    killtimer(CB.HNDL, %IDC_ATimer)
                    hATim = 0
                 END IF
                 IF hBTim THEN
                    killtimer(CB.HNDL, %IDC_BTimer)
                    hBTim = 0
                 END IF
                 IF hCTim THEN
                    killtimer(CB.HNDL, %IDC_CTimer)
                    hCTim = 0
                 END IF
    
    
    
      END SELECT
    
    
     END FUNCTION
    
    
    
    
    ​

  • #2
    That is like having a message block under CASE %WM_TIMER to run 3 different processes and all processes are to be done at different times intervals

    Comment


    • #3
      Checking the ID you assigned to the timer under the WM_TIMER message is correct.

      However, are you sure you need/want to use timers to accomplish whatever it is you wish to accomplish.

      Is this it?

      That is like having a message block under CASE %WM_TIMER to run 3 different processes and all processes are to be done at different times intervals
      That is, is your application some kind if "supervisory" program?

      If not, please describe that proposed application .
      Michael Mattias
      Tal Systems (retired)
      Port Washington WI USA
      [email protected]
      http://www.talsystems.com

      Comment


      • #4
        Thanks Michael, yes it kinda of supervisory to ensure a graphics control is progressively updated , check the status of a spinner cursor and check whether a another external program's
        computation has completed etc, looking at my skeleton code will it encounter problems later on?

        Comment


        • #5
          Tim-san, Just a suggestion, instead of the IF statements, use your ID's in a select case block like below.

          Code:
          CASE %WM_TIMER
              SELECT CASE CB.CTL
                  CASE %ID_TIMER1
                      '-ACTION JACKSON
                  CASE %ID_TIMER2
                      '-IS MY NAME!
                  CASE %ID_TIMER3
                      '-BOLD ADVENTURE
                  CASE %ID_TIMER4
                      '-IS MY GAME!
               END SELECT


          @looking at my skeleton code will it encounter problems later on?
          Keep in the back of your mind, the WM_TIMER message is a low priority messages.

          Comment


          • #6
            looking at my skeleton code will it encounter problems later on?
            Other than performance, no; looping to constantly check if something has happened is always less efficient than waiting for the something to happen.

            However, maybe you could have the external program set an event, and your program could wait on it in another thread and when the wait is satisfied you could post a private message to your dialog. That way you never do a 'check for ...' which comes up "No" AND your dialog could continue to do something useful without a needless interruption. That's what I would have done. (Actually, it's what I have done.)

            But I can guarantee using a timer and looping is easier to code and test.

            You get what you pay for.
            Michael Mattias
            Tal Systems (retired)
            Port Washington WI USA
            [email protected]
            http://www.talsystems.com

            Comment


            • #7
              Code:
                CASE %WM_TIMER
                  IF hAtim THEN​
              Does not work because hAtim will always be true no matter which timer caused the %WM_TIMER message.
              Code:
                CASE %WM_TIMER
                  SELECT CASE CB.CTL
                      CASE %ID_TIMER1​
              should be
              Code:
                CASE %WM_TIMER
                  SELECT CASE CB.WPARAM
                      CASE %ID_TIMER1​
              because the message is not %WM_COMMAND. CB.CTL happens to work for %WM_TIMER but is a bad habit to develope.
              See - CB Callback functions in Help.​

              Cheers,​
              Dale

              Comment


              • #8
                Originally posted by Tim Lakinir View Post
                ' Multiple Timers.bas
                ...
                CALLBACK FUNCTION DlgSpProc() AS LONG
                ...
                hAtim = settimer(CB.HNDL, %IDC_ATimer, 15, 0)
                hBtim = settimer(CB.HNDL, %IDC_BTimer, 150, 0)
                hCtim = settimer(CB.HNDL, %IDC_CTimer, 200, 0)
                ​...
                CASE %WM_TIMER
                IF hAtim THEN
                ' Do stuff A --- this is a ONE time process -- after this we kill the associated timer
                killtimer(CB.HNDL, %IDC_ATimer)
                hATim = 0​
                END IF

                IF hBtim THEN
                ' Do stuff B
                END IF
                IF hCtim THEN
                ' Do stuff C
                END IF
                [/CODE]
                Another example of poor grasp of fundamental logic as opposed to lack of programming skills.

                You will "Do stuff A" ,"Do stuff B" and "Do stuff "C" after 15 ms.

                From then on, you will "Do stuff B" and "Do stuff C"
                at 150,200,300,400,450,600 (twice),750, 800, 950 and 1000 milliseconds.

                See if you can work out why!

                Comment


                • #9
                  You will "Do stuff A" ,"Do stuff B" and "Do stuff "C" after 15 ms.

                  Not if he checks the Timer ID on WM_TIMER.

                  Of course, the pseudo-code in post #1 is not getting the Atim Btim and CTim correctly ; it's wParam ("CB.WPARAM").
                  Michael Mattias
                  Tal Systems (retired)
                  Port Washington WI USA
                  [email protected]
                  http://www.talsystems.com

                  Comment


                  • #10
                    Originally posted by Michael Mattias View Post

                    Not if he checks the Timer ID on WM_TIMER.
                    That was the whole point of my post which included his incorrect code. He doesn't!
                    Of course, the pseudo-code in post #1
                    What "pseudo-code" ? He posted actual PowerBASIC code, not "pseudocode".
                    s not getting the Atim Btim and CTim correctly
                    He IS "getting" them correctly, although he is not checking that they are valid
                    hAtim = settimer(CB.HNDL, %IDC_ATimer, 15, 0)will return %IDC_ATimer in hAtim (or 0 if it fails)
                    Of course there is real need to get them since he can just use %IDC_ATimer etc directly.
                    it's wParam ("CB.WPARAM").
                    He should be "checking" CB.WPARAM and comparing it to hAtim or %IDC_ATimer etc, but that's a different issue.




                    Comment


                    • #11
                      What "pseudo-code" ? He posted actual PowerBASIC code, not "pseudocode".
                      ???? 'Do Stuff A' and 'Do Stuff B' (even though commented out) are hardly actual (syntactically correct) PB Code. And commented out as shown creates and IF .. END IF block missing "imperative statement" which is allowed in the block construction but not in the single-line format. Regardless an IF.. END IF with nothing do is, if not illegal, silly; so let's call it pseudocode to avoid embarrasing Mr. Lakinir.

                      You may well be borderline 'pedantic-retentive.'
                      Michael Mattias
                      Tal Systems (retired)
                      Port Washington WI USA
                      [email protected]
                      http://www.talsystems.com

                      Comment


                      • #12
                        In post 1 is not pseudocode..
                        Dale

                        Comment


                        • #13
                          Originally posted by Michael Mattias View Post
                          ???? 'Do Stuff A' and 'Do Stuff B' (even though commented out) are hardly actual (syntactically correct) PB Code. And commented out as shown creates and IF .. END IF block missing "imperative statement" which is allowed in the block construction but not in the single-line format. Regardless an IF.. END IF with nothing do is, if not illegal, silly; so let's call it pseudocode to avoid embarrasing Mr. Lakinir.
                          No lets call it "unfinished application code". To avoid embarrassing ourselves.

                          The posted code compiles with no error in PBWin, so it is obviously not "pseudocode" by any standard definition.

                          Pseduocode dees NOT use commented out lines to identify actions. Commented lines are a clear indication of actual code that is intended to be compiled..

                          Conditionals such as If...END with no current callable instructions and containing comments indicating as yet undeveloped functions or or-line code are commonly used by any competent developer.

                          Since you are apparently unaware of this practice, let me explain:
                          ​Such constrcuts are used as "placeholders" so that other parts of the application can be compiled and tested. To do otherwise means that you can't do progressive/ unit testing during your development cycle,

                          Do you really not use this paradigm when developing?

                          Comment


                          • #14
                            Do you really not use this paradigm when developing?
                            Yes I do (or maybe I should say "When I did" )... and always called it pseudocode!
                            Michael Mattias
                            Tal Systems (retired)
                            Port Washington WI USA
                            [email protected]
                            http://www.talsystems.com

                            Comment


                            • #15
                              Originally posted by Michael Mattias View Post

                              Yes I do (or maybe I should say "When I did" )... and always called it pseudocode!
                              The correct term for that would be "skeleton code" or "dummy code". Pseudocode is something very different.

                              https://en.wikipedia.org/wiki/Pseudocode.

                              https://en.wikipedia.org/wiki/Skelet...er_programming)

                              Comment

                              Working...
                              X