Page 1 of 1

Timer, TimeOut

Posted: Fri Mar 07, 2008 11:42 pm
by frederic
An example of a function called every second

Code: Select all

; http://library.gnome.org/devel/glib/stable/glib-The-Main-Event-Loop.html#g-timeout-add

Global stoptimer = #False

Procedure MyTimeOutFunction(*pdata.l)
  Debug "hello"
  If stoptimer = #False
    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure

If OpenWindow(0, 0, 0, 300, 50, "TimeOut example...", #PB_Window_SystemMenu | #PB_Window_ScreenCentered)

  If CreateGadgetList(WindowID(0))
    ButtonGadget(1, 10, 10, 280, 30, "Stop timer")
  EndIf 
  
  g_timeout_add_(1000, @MyTimeOutFunction(), #Null)
  
  Repeat
  
  If EventGadget() = 1
    stoptimer = #True
    DisableGadget(1, 1)
  EndIf
  
  Until WaitWindowEvent()=#PB_Event_CloseWindow
EndIf
08/03/08 - change gtk_timeout_add to g_timeout_add

Posted: Sat Mar 08, 2008 5:27 am
by Beach
Hey thats cool... thanks! :)

Posted: Sat Mar 08, 2008 1:24 pm
by dhouston
The GTK+ Reference Manual says gtk_timeout_add has been deprecated since version 2.4.

Posted: Sat Mar 08, 2008 1:49 pm
by frederic
Thanks for this precision dhouston, changes made

Posted: Sun Mar 09, 2008 8:44 pm
by flaith
Very usefull, thanks

Posted: Tue Apr 22, 2008 7:49 pm
by Straker
I'm using PureBasic 4.10 and getting an invalid memory access on this line:

Code: Select all

Procedure MyTimeOutFunction(*pdata.l)
  Debug "hello"
  If stoptimer = #False
-->    ProcedureReturn #True
  Else
    ProcedureReturn #False
  EndIf
EndProcedure
Any ideas?

Posted: Wed Apr 23, 2008 1:06 am
by walker
I guess you haven't opened a window before this error occurs... so simply use gtk_init_(0,0) at the top of you pgm( it doesn't hurt anything) and it will work...

EDIT:
:oops: didn't read your post correctly... you're using the example from the first post?

Posted: Wed Apr 23, 2008 7:27 pm
by Straker
Thanks walker.

Yes, I am using the example code at the top, guess I didn't make that clear.

I did try your suggestion, however, still getting the invalid memory access.

I am using Ubuntu Breezy, and will upgrade real soon, but could that be a factor?

Thanks.

Posted: Wed Apr 23, 2008 7:31 pm
by Straker
UPDATE: Found the solution:

Changed "Procedure" to "ProcedureC" and voila.

Thanks for your help.