Installing a timer

Mac OSX specific forum
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Installing a timer

Post 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
Niffo
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Installing a timer

Post 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)
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Re: Installing a timer

Post 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.
Niffo
User avatar
Shardik
Addict
Addict
Posts: 2058
Joined: Thu Apr 21, 2005 2:38 pm
Location: Germany

Re: Installing a timer

Post 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...:)
Niffo
Enthusiast
Enthusiast
Posts: 504
Joined: Tue Jan 31, 2006 9:43 am
Location: France

Re: Installing a timer

Post 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 ;)
Niffo
Post Reply