PlanetSquires Forums

Please login or register.

Login with username, password and session length
Advanced search  

Author Topic: Using a Timer in Freebasic  (Read 3367 times)

Andrew Lindsay

  • Junior Member
  • **
  • Posts: 76
Using a Timer in Freebasic
« on: January 14, 2016, 07:00:54 PM »

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
Code: [Select]

'--------------------------------------------------------------------------------
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
Logged

David Kenny

  • Senior Member
  • ***
  • Posts: 452
  • Windows 7
Re: Using a Timer in Freebasic
« Reply #1 on: January 14, 2016, 09:23:19 PM »

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
Logged

Andrew Lindsay

  • Junior Member
  • **
  • Posts: 76
Re: Using a Timer in Freebasic
« Reply #2 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.


Regards


Andrew
Logged

David Kenny

  • Senior Member
  • ***
  • Posts: 452
  • Windows 7
Re: Using a Timer in Freebasic
« Reply #3 on: January 15, 2016, 03:54:28 AM »

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
Logged

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 8762
  • Windows 10
    • PlanetSquires Software
Re: Using a Timer in Freebasic
« Reply #4 on: January 15, 2016, 10:20:37 AM »

Is this what you're trying to do?

Code: [Select]
'--------------------------------------------------------------------------------
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
« Last Edit: January 15, 2016, 10:44:41 AM by TechSupport »
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Paul Squires

  • Administrator
  • Guru Member
  • *****
  • Posts: 8762
  • Windows 10
    • PlanetSquires Software
Re: Using a Timer in Freebasic
« Reply #5 on: January 15, 2016, 10:55:50 AM »

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

Code: [Select]
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

« Last Edit: January 15, 2016, 10:58:45 AM by TechSupport »
Logged
Paul Squires
PlanetSquires Software
WinFBE Editor and Visual Designer

Andrew Lindsay

  • Junior Member
  • **
  • Posts: 76
Re: Using a Timer in Freebasic
« Reply #6 on: January 15, 2016, 12:59:21 PM »

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
Logged