Page 1 of 1

Installing a timer

Posted: Tue Jun 09, 2009 2:42 pm
by Niffo
This code causes an "Invalid memory access", do you know why ?

Code: Select all


#kEventDurationSecond = 1.0

OpenWindow(0, 100,100, 300, 200, "Test Window")
ProcedureDLL CallBackTimerHandler(theTimer, *userData)
   Debug "Timer ="
EndProcedure

mainLoop = GetMainEventLoop_()
timerUPP = NewEventLoopTimerUPP_(@CallBackTimerHandler())
InstallEventLoopTimer_(mainLoop, 5 * #kEventDurationSecond, #kEventDurationSecond, timerUPP, #Null, @theTimer)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

Re: Installing a timer

Posted: Fri Feb 25, 2011 10:40 pm
by Shardik
Niffo wrote:This code causes an "Invalid memory access", do you know why ?
1.) The API function InstallEventLoopTimer_() seems to be defined incorrectly in PB.
Therefore I have imported this function and defined the 2nd and 3rd parameter as
type Double.
2.) The Callback has to be defined as ProcedureCDLL.
3.) You always should remove your timer afterwards with RemoveEventLoopTimer().

Code: Select all

ImportC ""
  InstallEventLoopTimer(EventLoopRef.L, FireDelay.D, Interval.D, TimerProc.L, *UserData, *EventLoopTimerRef)
  RemoveEventLoopTimer(EventLoopTimerRef.L)
EndImport

#kEventDurationSecond = 1.0

OpenWindow(0, 100,100, 300, 200, "Test Window")
ProcedureCDLL CallBackTimerHandler(theTimer, *userData)
   Debug "Timer ="
EndProcedure

mainLoop = GetMainEventLoop_()
timerUPP = NewEventLoopTimerUPP_(@CallBackTimerHandler())
InstallEventLoopTimer(mainLoop, #kEventDurationSecond, #kEventDurationSecond, timerUPP, #Null, @theTimer)

Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow

RemoveEventLoopTimer(theTimer)

Re: Installing a timer

Posted: Mon Mar 07, 2011 8:02 pm
by Niffo
Thank you VERY much for this (3 year later) answer ! :-)
Now "Window Timers" are implemented in PureBasic, but it is a good thing to know why this API was not working. I never thought his PB declaration could be incorrect.
I bypassed my problem using threads, but it will be interesting to see if this timer locks when moving the window and so on ... (like the PB "Window Timers") or not.

Re: Installing a timer

Posted: Tue Mar 08, 2011 7:34 pm
by Shardik
Niffo wrote:Thank you VERY much for this (3 year later) answer ! :-)
I am glad that you still appreciate my problem solution. Although it hasn't been
3 years but only 20 months later. :wink:
I am still a beginner with MacOS X. I own my first Mac since September 2010, so
I didn't have the opportunity to examine your timer problem at the time you
posted it...:)

Re: Installing a timer

Posted: Thu Mar 10, 2011 10:08 am
by Niffo
Oh yes, sorry : it's only 2 years !
I too own my first mac when i posted the original message of this thread. Welcome in the Steve's (very) closed community ;)