Simple timer?
Simple timer?
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
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
Sounds like a thread is the best bet:
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.
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
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.
Help
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
Can you give an example where this code has been used with one form and couple of MessageRequests which just prompts for something?
/Ekix
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
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
Last edited by netmaestro on Mon Jan 01, 2007 9:08 pm, edited 1 time in total.
BERESHEIT
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:
**EDIT: woa, too slow! 
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

I may look like a mule, but I'm not a complete ass.
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
http://www.purebasic.fr/english/viewtopic.php?t=24054
Thanks a lot
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
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.
- netmaestro
- PureBasic Bullfrog
- Posts: 8451
- Joined: Wed Jul 06, 2005 5:42 am
- Location: Fort Nelson, BC, Canada
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
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???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.
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.