• Welcome to PlanetSquires Forums.
 

Using a Timer in Freebasic

Started by Andrew Lindsay, January 14, 2016, 07:00:54 PM

Previous topic - Next topic

Andrew Lindsay

Paul and group,


I'm trying a simple example of getting a timer to fire every second, like a similar example in Firefly for PB.


here is the minimal code


'--------------------------------------------------------------------------------
Function FORM1_BTNSTART_BN_CLICKED ( _
                                   ControlIndex     as Integer, _   ' index in Control Array
                                   hWndForm         as HWnd, _      ' handle of Form
                                   hWndControl      as HWnd, _      ' handle of Control
                                   idButtonControl  as Integer   _  ' identifier of button
                                   ) as Integer
 
   SetTimer (HWND_FORM1,HWND_FORM1_TIMER1, 1000, Null)   'Set the timer to 1 full second again
   Function =1   ' change according to your needs
End Function




'--------------------------------------------------------------------------------
Function FORM1_TIMER1_WM_TIMER ( _
                               hWndForm      as HWnd, _     ' handle of Form
                               wTimerID      as Integer  _  ' the timer identifier
                               ) as Integer
   Print "Tock"
   Function = 0   ' change according to your needs
End Function



I am printing out to the console and it is printing as fast as the console can fill, not every second as I would anticipate.


Regards


Andrew

David Kenny

Andrew,

Google search:  "MSDN SetTimer"
Your arguments need to look more like this:

SetTimer (hWndForm, IDC_FORM1_TIMER1, 1000)   'Set the timer to 1 full second again

David

Andrew Lindsay

That's well as maybe, but unfortunately, FreeBasic expects it to have an additional argument either Null or a pointer to a specific timer handling function.


If you use the NULL argument, then the standard WM timer event is called.


Regards


Andrew

David Kenny

Quote from: Andrew Lindsay on January 14, 2016, 09:53:47 PM
That's well as maybe, but unfortunately, FreeBasic expects it to have an additional argument either Null or a pointer to a specific timer handling function.

If you use the NULL argument, then the standard WM timer event is called.

As you can see at MSDN the last argument is optional.  I don't use FreeBasic, so it is conceivable that they didn't make it optional in their includes.  Silly if they didn't.  The Null option is what you want however as that is the way FF is written if you are using FF's Timer "control".  It would appear that you are, because the name of the timer suggests that to be the case.

By "standard WM timer event" you as you say, that just means the timer event gets sent to the form's regular message loop, along with the messages from the form's controls.

The important part of my post was to point out that one of the things you were sending was wrong (the timer handle needed to be the timer ID).  The last argument should be Null if the function definition for SetTimer was defined differently than documented at MSDN, and can be left off if it is defined correctly.

Best regards,

David

Paul Squires

#4
Is this what you're trying to do?


'--------------------------------------------------------------------------------
Function FORM1_TIMER1_WM_TIMER ( _
                               hWndForm      as HWnd, _     ' handle of Form
                               wTimerID      as Long  _  ' the timer identifier
                               ) as Long

   Static i as Long
   
   i = i + 1
   Print i;
   
   Function = 0   ' change according to your needs
End Function


'--------------------------------------------------------------------------------
Function FORM1_BTNSTART_BN_CLICKED ( _
                                   ControlIndex     as Long, _      ' index in Control Array
                                   hWndForm         as HWnd, _      ' handle of Form
                                   hWndControl      as HWnd, _      ' handle of Control
                                   idButtonControl  as Long   _     ' identifier of button
                                   ) as Long

   ' Change timer interval. You need to kill the original timer
   ' and create a new timer with the new interval. In this
   ' case the new interval is 2 seconds
   KillTimer hWndForm, IDC_FORM1_TIMER1
   SetTimer( hWndForm, IDC_FORM1_TIMER1, 2000, Null )       

   Function = 0   ' change according to your needs
End Function


'--------------------------------------------------------------------------------
Function FORM1_BTNSTOP_BN_CLICKED ( _
                                  ControlIndex     as Long, _      ' index in Control Array
                                  hWndForm         as HWnd, _      ' handle of Form
                                  hWndControl      as HWnd, _      ' handle of Control
                                  idButtonControl  as Long   _     ' identifier of button
                                  ) as Long

   ' Stop timer
   KillTimer hWndForm, IDC_FORM1_TIMER1

   Function = 0   ' change according to your needs
End Function

Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

#5
Here is an example using the Timer API directly (bypassing the use of the built-in Firefly Timer control):


Dim Shared idTimer as Long = 1000

'--------------------------------------------------------------------------------
Function FORM1_BTNSTART_BN_CLICKED ( _
                                   ControlIndex     as Long, _      ' index in Control Array
                                   hWndForm         as HWnd, _      ' handle of Form
                                   hWndControl      as HWnd, _      ' handle of Control
                                   idButtonControl  as Long   _     ' identifier of button
                                   ) as Long

   ' Change timer interval. You need to kill the original timer
   ' and create a new timer with the new interval. In this
   ' case the new interval is 1 second
   KillTimer hWndForm, idTimer
   SetTimer( hWndForm, idTimer, 1000, Cast(TimerProc, @MyTimer) )       

   Function = 0   ' change according to your needs
End Function


'--------------------------------------------------------------------------------
Function FORM1_BTNSTOP_BN_CLICKED ( _
                                  ControlIndex     as Long, _      ' index in Control Array
                                  hWndForm         as HWnd, _      ' handle of Form
                                  hWndControl      as HWnd, _      ' handle of Control
                                  idButtonControl  as Long   _     ' identifier of button
                                  ) as Long

   ' Stop timer
   KillTimer hWndForm, idTimer

   Function = 0   ' change according to your needs
End Function


'--------------------------------------------------------------------------------
Function MyTimer( ByVal hWndTimer as HWnd, _
                  ByVal uMsg as Long, _
                  ByVal idEvent as Long, _
                  ByVal dwTime as Long _
                  ) as Long
                   
   Static i as Long
   
   i = i + 1
   Print i;
   
   Function = 0
   
End Function


Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Andrew Lindsay

Cheers,


I was trying to use the included Timer control in FF/FB, but couldn't get it to work.  I'll try without having the timer control.


Thanks for all your help and and confusion/frustration I may have caused or displayed.


best regards


Andrew