Simple timer?

Just starting out? Need help? Post your questions and find answers here.
ekix
User
User
Posts: 34
Joined: Thu Apr 21, 2005 7:43 pm
Location: Finland
Contact:

Simple timer?

Post 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
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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.
I may look like a mule, but I'm not a complete ass.
ekix
User
User
Posts: 34
Joined: Thu Apr 21, 2005 7:43 pm
Location: Finland
Contact:

Help

Post 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
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.
Last edited by netmaestro on Mon Jan 01, 2007 9:08 pm, edited 1 time in total.
BERESHEIT
srod
PureBasic Expert
PureBasic Expert
Posts: 10589
Joined: Wed Oct 29, 2003 4:35 pm
Location: Beyond the pale...

Post 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! :)
I may look like a mule, but I'm not a complete ass.
georgej
User
User
Posts: 11
Joined: Wed Aug 17, 2005 5:21 pm

Post 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
ekix
User
User
Posts: 34
Joined: Thu Apr 21, 2005 7:43 pm
Location: Finland
Contact:

Thanks a lot

Post 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
Last edited by ekix on Mon Jan 01, 2007 9:24 pm, edited 1 time in total.
User avatar
netmaestro
PureBasic Bullfrog
PureBasic Bullfrog
Posts: 8451
Joined: Wed Jul 06, 2005 5:42 am
Location: Fort Nelson, BC, Canada

Post 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.)
BERESHEIT
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post by Kale »

Remember to search for stuff before asking:

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

Image
G-PB
User
User
Posts: 11
Joined: Sat Jan 22, 2005 7:33 pm

Post 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.
User avatar
Fangbeast
PureBasic Protozoa
PureBasic Protozoa
Posts: 4791
Joined: Fri Apr 25, 2003 3:08 pm
Location: Not Sydney!!! (Bad water, no goats)

Post 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
Amateur Radio/VK3HAF, (D-STAR/DMR and more), Arduino, ESP32, Coding, Crochet
Kale
PureBasic Expert
PureBasic Expert
Posts: 3000
Joined: Fri Apr 25, 2003 6:03 pm
Location: Lincoln, UK
Contact:

Post 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.
--Kale

Image
rsts
Addict
Addict
Posts: 2736
Joined: Wed Aug 24, 2005 8:39 am
Location: Southwest OH - USA

Post 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
Derek
Addict
Addict
Posts: 2354
Joined: Wed Apr 07, 2004 12:51 am
Location: England

Post 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.
G-PB
User
User
Posts: 11
Joined: Sat Jan 22, 2005 7:33 pm

Post 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.
Post Reply