Timer question using threads
Posted: Tue Aug 15, 2006 6:51 pm
I've written this simple timer to avoid a burning disaster in my kitchen.
After testing this code (could be better code, I know
) a very strange thing occurs:
Most of the time it works fine but then randomly when clicking on the start button the timer goes off immediately and the timer doesn't run.
Can it be that I overlook something or have I misunderstood the thread thing. (I enabled threadsafe when compiling)
Most of the code:
After testing this code (could be better code, I know

Most of the time it works fine but then randomly when clicking on the start button the timer goes off immediately and the timer doesn't run.
Can it be that I overlook something or have I misunderstood the thread thing. (I enabled threadsafe when compiling)
Most of the code:
Code: Select all
;- Window Constants
Enumeration
#Window_0
#Window_1
EndEnumeration
;- Gadget Constants
Enumeration
#Button_0
#Button_1
#Frame3D_2
#Combo_0
#Image_0
#Sound_0
#Sound_1
#ProgressBar_0
#Text_1
#Text_2
#Text_3
#Editor_1
EndEnumeration
;- Fonts Global
Global FontID1
FontID1 = LoadFont(1, "Lucida Console", 72)
Global FontID2
FontID2 = LoadFont(2, "Lucida Console", 10, #PB_Font_Bold)
Global FontID3
FontID3 = LoadFont(3, "Lucida Console", 8)
;- Image Global
Global Image0
;- Catch Image
Image0 = CatchImage(0, ?Image0)
;- Image Inclusion
DataSection
Image0:
IncludeBinary "Beeld\Klok.ico"
EndDataSection
;- Sound Global
Global Sound0
Global Sound1
;- Catch Sound
If InitSound()=0
MessageRequester("Fout audio systeem","Audio omgeving kan niet geïnitaliseerd worden.", #MB_ICONERROR)
End
EndIf
Sound0 = CatchSound(#Sound_0,?Sound0)
Sound1 = CatchSound(#Sound_1,?Sound1)
;- Sound Inclusion
DataSection
Sound0:
IncludeBinary "Geluid\Crick.wav"
Sound1:
IncludeBinary "Geluid\Pinc.wav"
EndDataSection
;- Globals & Declarations
Global Insteltijd.s
Global Wekkerklaar.b=0
Global Wekkermin.b=0
Global Wekkersec.b=0
Global Totaalsec.w=0
Global Voortgang.f=0
Global Alarmstop.b=0
;- Thread Global
Global WekkertjeThreadID
;- Procedures
Procedure Open_Window_0()
If OpenWindow(#Window_0, 438, 291, 250, 200, "'t Kook Wekkertje ", #PB_Window_SystemMenu | #PB_Window_MinimizeGadget | #PB_Window_TitleBar | #PB_Window_ScreenCentered )
SetWindowPos_(WindowID(#Window_0), #HWND_TOPMOST, 0, 0, 0, 0, #SWP_NOMOVE | #SWP_NOSIZE)
If CreateGadgetList(WindowID(#Window_0))
ButtonGadget(#Button_0, 10, 170, 170, 20, ">> <<")
ButtonGadget(#Button_1, 190, 170, 50, 20, "?")
SetGadgetFont(#Button_1, FontID3)
Frame3DGadget(#Frame3D_2, 10, 10, 170, 150, "", #PB_Frame3D_Single)
ComboBoxGadget(#Combo_0, 190, 60, 50, 20, #PB_ComboBox_LowerCase)
ImageGadget(#Image_0, 200, 10, 32, 32, Image0)
ProgressBarGadget(#ProgressBar_0, 210, 100, 10, 60, 0, 10, #PB_ProgressBar_Vertical)
AddGadgetItem(#Combo_0,-1,"1")
For Tijd.b = 2 To 60
AddGadgetItem(#Combo_0,-1,Str(Tijd))
Next
SetGadgetState(#Combo_0, 0)
TextGadget(#Text_1, 30, 30, 130, 80, "00", #PB_Text_Center)
SetGadgetFont(#Text_1, FontID1)
TextGadget(#Text_2, 150, 140, 20, 10, "00", #PB_Text_Right)
SetGadgetFont(#Text_2, FontID2)
TextGadget(#Text_3, 20, 140, 50, 10, "0 min")
SetGadgetFont(#Text_3, FontID3)
EndIf
EndIf
EndProcedure
Procedure Wekkertje_Start(Unused)
For Wekkermin = 1 To Val(Insteltijd)
For Wekkersec = 1 To 60
Delay(1000)
If Wekkersec<10 : Wekkerseconde.s="0"+Str(Wekkersec) : Else : Wekkerseconde=Str(Wekkersec) : EndIf
If Wekkersec<60
SetGadgetText(#Text_2, Wekkerseconde)
ElseIf Wekkersec=60
SetGadgetText(#Text_2, "00")
EndIf
SetGadgetFont(#Text_2, FontID2)
Totaalsec+1
Voortgang=(Totaalsec/(Val(Insteltijd)*60))*10
SetGadgetState(#ProgressBar_0, Int(Voortgang))
Next
If Wekkermin<10 : Wekkerminuut.s="0"+Str(Wekkermin) : Else : Wekkerminuut=Str(Wekkermin) : EndIf
SetGadgetText(#Text_1, Wekkerminuut)
SetGadgetFont(#Text_1, FontID1)
Next
Wekkerklaar=1
For Lokaal.b=1 To 120
If Alarmstop=1
Alarmstop=0
Break
EndIf
PlaySound(#Sound_1,0)
Delay(500)
Next
EndProcedure
Procedure Wekkertje_Stop(Unused)
Wekkermin=1
Wekkersec=0
Totaalsec=0
SetGadgetText(#Text_1, "00")
SetGadgetFont(#Text_1, FontID1)
SetGadgetText(#Text_2, "00")
SetGadgetFont(#Text_2, FontID2)
SetGadgetState(#ProgressBar_0, 0)
EndProcedure
;- Set Thread
WekkertjeThreadID=CreateThread(@Wekkertje_Start(),0)
If WekkertjeThreadID=0
MessageRequester("Fout in thread aanroep", "Kan de klok in het programma niet weergeven door een threading fout.", #MB_ICONERROR)
EndIf
PauseThread(WekkertjeThreadID)
;- Main
Knoptrigger.b=0
Open_Window_0()
;- Loop
Repeat
Event = WaitWindowEvent() ; This line waits until an event is received from Windows
WindowID = EventWindow() ; The Window where the event is generated, can be used in the gadget procedures
GadgetID = EventGadget() ; Is it a gadget event?
EventType = EventType() ; The event type
If Event = #PB_Event_Gadget
If GadgetID = #Button_0
If Knoptrigger=0
If Wekkerklaar=0
Delay(10)
ResumeThread(WekkertjeThreadID)
ElseIf Wekkerklaar =1
WekkertjeThreadID=CreateThread(@Wekkertje_Start(),0)
Wekkerklaar=0
EndIf
Insteltijd=GetGadgetText(#Combo_0)
DisableGadget(#Combo_0, 1)
SetGadgetText(#Text_3, Insteltijd+" min")
SetGadgetFont(#Text_3, FontID3)
Knoptrigger=1
PlaySound(#Sound_0,0)
ElseIf Knoptrigger=1
If Wekkerklaar=0
PauseThread(WekkertjeThreadID)
EndIf
If Wekkerklaar=1
Alarmstop=1
EndIf
DisableGadget(#Combo_0, 0)
Wekkertje_Stop(Unused)
SetGadgetText(#Text_3, "0 min")
SetGadgetFont(#Text_3, FontID3)
Knoptrigger=0
PlaySound(#Sound_0,0)
EndIf
ElseIf GadgetID = #Button_1
HulpWindow(Terug)
ElseIf GadgetID = #Combo_0
ElseIf GadgetID = #Image_0
ElseIf GadgetID = #ProgressBar_0
EndIf
EndIf
Until Event = #PB_Event_CloseWindow
End