Page 1 of 2

Simple timer?

Posted: Mon Jan 01, 2007 8:27 pm
by ekix
Hello guys

I tried to find a simple timer here without success. Anyway the timer should work like in the back, and create a MessageRequester for example every 10 seconds. The time should be able to change while the program is running, like first 10 seconds, then 9.....1 seconds, and the program's gadgets (buttons etc...) should be in use while the timer runs.

I could not find this kind of example neither from help-file nor here.

In case someone can advice. I'd be grateful :=)

/Ekix

Posted: Mon Jan 01, 2007 8:42 pm
by srod
Sounds like a thread is the best bet:

Code: Select all

Global time=10 ;10 seconds

Procedure Thread(Parameter)

  Repeat
    Delay(time*1000)
    MessageRequester("Hey ho", Str(time)+" seconds are up!", 0)
  ForEver

EndProcedure

CreateThread(@Thread(), 0)
Repeat
ForEver
To alter the delay, simply change the value of the global variable 'time' etc.

Alternatively, have a look at the SetTimer_() function in the win API etc.

Help

Posted: Mon Jan 01, 2007 9:01 pm
by ekix
I could not getting it working, instead it crashed my project.
Can you give an example where this code has been used with one form and couple of MessageRequests which just prompts for something?

/Ekix

Posted: Mon Jan 01, 2007 9:05 pm
by netmaestro

Code: Select all

;====================================================
; Program:        Some Timer Examples
; Author:         netmaestro
; Date:           January 1, 2007
;====================================================


Global SecondCount_1 = 30, SecondCount_2 = 30, SecondCount_3 = 30

Procedure CountDown_1(void)
  Repeat
    Delay(1000)
    If SecondCount_1 > 0
      SecondCount_1 - 1
    EndIf
  ForEver
EndProcedure

Procedure CountDown_2(uID, uMsg, dwUser, dw1, dw2)
  If SecondCount_2 > 0
    SecondCount_2 - 2
  EndIf
EndProcedure

Procedure CountDown_3()
  If SecondCount_3 > 0
    SecondCount_3 - 3
  EndIf
EndProcedure

OpenWindow(0,0,0,320,240,"Timer Options",$CF0001)
CreateGadgetList(WindowID(0))
TextGadget(1, 145, 50, 100, 20, "")
TextGadget(2, 145, 80, 100, 20, "")
TextGadget(3, 145, 110, 100, 20, "")

CreateThread(@CountDown_1(), 0)                         ; Example 1: Ordinary Thread
timeSetEvent_(1000,10,@CountDown_2(),0,#TIME_PERIODIC)  ; Example 2: timeSetEvent_() 
SetTimer_(WindowID(0), 1, 1000, @CountDown_3())         ; Example 3: SetTimer_()


Repeat
  EventID = WaitWindowEvent(20)
  
  If Str(SecondCount_1) <> GetGadgetText(1)
    SetGadgetText(1, Str(SecondCount_1))
  EndIf

  If Str(SecondCount_2) <> GetGadgetText(2)
    SetGadgetText(2, Str(SecondCount_2))
  EndIf

  If Str(SecondCount_3) <> GetGadgetText(3)
    SetGadgetText(3, Str(SecondCount_3))
  EndIf
Until EventID = #WM_CLOSE

End
All of these alternatives are executed asynchronously, so the main loop can continue processing window and gadget events with no holdup.

Posted: Mon Jan 01, 2007 9:07 pm
by srod
Yes the example wasn't intended to be used within a current application as it just runs and runs until you close it with the debugger.

Try the following:

Code: Select all

Global time=10 ;10 seconds

Procedure AlertThread(Parameter)

  Repeat
    Delay(time*1000)
    MessageRequester("Hey ho", Str(time)+" seconds are up!", 0)
  ForEver

EndProcedure

CreateThread(@AlertThread(), 0)

If OpenWindow(0, 0, 0, 222, 200, "ButtonGadgets", #PB_Window_SystemMenu | #PB_Window_ScreenCentered) And CreateGadgetList(WindowID(0))
  ButtonGadget(0, 10, 10, 200, 20, "Standard Button")
  ButtonGadget(1, 10, 40, 200, 20, "Left Button", #PB_Button_Left)
  ButtonGadget(2, 10, 70, 200, 20, "Right Button", #PB_Button_Right)
  ButtonGadget(3, 10,100, 200, 60, "Multiline Button  (longer text gets automatically wrapped)", #PB_Button_MultiLine)
  ButtonGadget(4, 10,170, 200, 20, "Toggle Button", #PB_Button_Toggle)
  Repeat : Until WaitWindowEvent() = #PB_Event_CloseWindow
EndIf
**EDIT: woa, too slow! :)

Posted: Mon Jan 01, 2007 9:18 pm
by georgej
One of my first learner projects was a simple reaction timer that I posted on the forum, if this is of any help to you. I did comment the code.

http://www.purebasic.fr/english/viewtopic.php?t=24054

Thanks a lot

Posted: Mon Jan 01, 2007 9:21 pm
by ekix
Both of them work's fine. The second one is more suitable for me :=)

This is a Gooood forum, answers come in few minutes even for newbies like me.

Thanks/
Ekix

Posted: Mon Jan 01, 2007 9:22 pm
by netmaestro
It should be pointed out that of the three options in my example, only the timeSetEvent_() is capable of single-millisecond precision. The Ordinary Thread version is using Delay() which has a minimum resolution of 15 milliseconds, and the same is true for SetTimer_(). The Ordinary Thread version could be modified to make use of timeBeginPeriod_() and timeEndPeriod_() which can bring the precision of Sleep_() down to 1 ms, or the use of QueryPerformanceCounter_() can bring it down much lower, as fine as 1 microsecond (1 millionth of a second.)

Posted: Tue Jan 02, 2007 12:23 am
by Kale
Remember to search for stuff before asking:

http://www.purebasic.fr/english/viewtopic.php?t=21988

Posted: Tue Jan 02, 2007 5:50 am
by G-PB
Perhaps you didn't read when he wrote "I could not find this kind of example neither from help-file nor here." And just because a search function exists, it doesn't necessarily mean that answers to particular questions can be found so easily. How you word your search plays a big part in what results are returned. Rather than complain about the search function not being used (when it is), why don't you enlighten others of what the search string was that you used to get the answers for what they needed.

Posted: Tue Jan 02, 2007 6:14 am
by Fangbeast
Actually, srod, sparkie and netmaestro are threads running in the background of the forum and I am glad to have encountered them globally myself on occasion when the forum search was quite useless:D :D :D :D :D :D :D

Posted: Tue Jan 02, 2007 2:21 pm
by Kale
G-PB wrote:Perhaps you didn't read when he wrote "I could not find this kind of example neither from help-file nor here." And just because a search function exists, it doesn't necessarily mean that answers to particular questions can be found so easily. How you word your search plays a big part in what results are returned. Rather than complain about the search function not being used (when it is), why don't you enlighten others of what the search string was that you used to get the answers for what they needed.
He didn't find anything? Reeeeeaaaaally???

The search string i used was 'Simple Timer', which is exactly this thread's title. The fifth thread returned called 'Timer question using threads' contains examples (and more) shown in this thread. The seventh returned thread is called 'how do you create a timer?' (How much more informative can you get) which also contains the examples contined in this thread.

http://www.purebasic.fr/english/viewtopic.php?t=23200
http://www.purebasic.fr/english/viewtopic.php?t=21988

New users who usually say they cant find anything, havn't really looked. This forum contains almost every question (and answer) that a new user might have.

Posted: Tue Jan 02, 2007 2:47 pm
by rsts
I agree with Kale.

Learning to properly use the seach will make the forums an infinately more valuable resource.

Everyone should learn 'how' to search it.

cheers

Posted: Tue Jan 02, 2007 3:23 pm
by Derek
Fangbeast wrote:Actually, srod, sparkie and netmaestro are threads running in the background of the forum...
Very funny, conjures up visions of Tron.

Posted: Tue Jan 02, 2007 5:47 pm
by G-PB
@Kale... reeeeeaaaaaalllllly? Yes. Really. That's what he said. Did you read?

@Rsts: That is exactly my point. Rather than knock someone for not using a function (that they did attempt to use) show them how to use it properly.