Threads and multiple parameters....
Posted: Thu Jun 16, 2005 11:24 pm
Threads and multiple parameters.... how?
http://www.purebasic.com
https://www.purebasic.fr/english/
i come here with a small example on how to 'pass'use a single parameter as a pointer to a structure with whatever parameters you want.
Fill a structure with the parameters for the job the thread is designed to do and pass a pointer to it to the thread.
Code: Select all
;-
;- Purpose: PB Threads, Passing structures as argument.
;- In order to use multiple arguments.
;-
;- Author: Flype (2005, june)
;- Example: 3 different 'threaded' little clocks.
;-
Structure CLOCK
gadget.l
gmt.b
mask1.s
mask2.s
delay.l
EndStructure
Procedure ClockThread(*a.CLOCK)
If *a\mask2 = ""
*a\mask2 = *a\mask1
EndIf
Repeat
tick = 1 - tick
If tick
SetGadgetText(*a\gadget, FormatDate(*a\mask1, AddDate(Date(), #PB_Date_Hour, *a\gmt)))
Else
SetGadgetText(*a\gadget, FormatDate(*a\mask2, AddDate(Date(), #PB_Date_Hour, *a\gmt)))
EndIf
Delay(*a\delay)
ForEver
EndProcedure
OpenWindow(0,0,0,260,145,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Thread")
CreateGadgetList(WindowID())
TextGadget(0,5, 5,250,40,"",#PB_Text_Center)
TextGadget(1,5, 50,250,40,"",#PB_Text_Center)
TextGadget(2,5,100,250,40,"",#PB_Text_Center)
LoadFont(0,"Arial",24,#PB_Font_Bold)
SetGadgetFont(0,FontID())
SetGadgetFont(1,FontID())
SetGadgetFont(2,FontID())
Clock0.CLOCK
Clock0\gadget = 0
Clock0\gmt = 0
Clock0\delay = 1000
Clock0\mask1 = "%hh:%ii:%ss"
Clock0\mask2 = "%hh:%ii %ss"
CreateThread(@ClockThread(),@Clock0)
Clock1.CLOCK
Clock1\gadget = 1
Clock1\gmt = 1
Clock1\delay = 1000
Clock1\mask1 = "%hhh %iim %sss"
CreateThread(@ClockThread(),@Clock1)
Clock2.CLOCK
Clock2\gadget = 2
Clock2\gmt = 2
Clock2\delay = 1000
Clock2\mask1 = "%hhh %iim %sss"
CreateThread(@ClockThread(),@Clock2)
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
Code: Select all
;-
;- Purpose: PB Threads, Passing structures as argument.
;- In order to use multiple arguments.
;-
;- Author: Flype (2005, june)
;- Example: 3 different 'threaded' little clocks.
;-
Structure CLOCK
threadId.l
gadgetId.l
timeGMT.b
timeDelay.l
timeMask1.s
timeMask2.s
EndStructure
NewList Clock.CLOCK()
Procedure ClockThread(*this.CLOCK)
Repeat
tick = 1 - tick
time = AddDate(Date(), #PB_Date_Hour, *this\timeGMT)
If tick
SetGadgetText(*this\gadgetId, FormatDate(*this\timeMask1, time))
Else
SetGadgetText(*this\gadgetId, FormatDate(*this\timeMask2, time))
EndIf
Delay(*this\timeDelay)
ForEver
EndProcedure
Procedure SetClock(gadget.l,gmt.l,delay.l,mask1.s,mask2.s)
If AddElement(Clock())
If mask2 = ""
mask2 = mask1
EndIf
Clock()\gadgetId = gadget
Clock()\timeGMT = gmt
Clock()\timeDelay = delay
Clock()\timeMask1 = mask1
Clock()\timeMask2 = mask2
Clock()\threadId = CreateThread(@ClockThread(),@Clock())
EndIf
EndProcedure
OpenWindow(0,0,0,260,145,#PB_Window_SystemMenu|#PB_Window_ScreenCentered,"Thread")
CreateGadgetList(WindowID())
TextGadget(0,5, 5,250,40,"",#PB_Text_Center)
TextGadget(1,5, 50,250,40,"",#PB_Text_Center)
TextGadget(2,5,100,250,40,"",#PB_Text_Center)
LoadFont(0,"Arial",24,#PB_Font_Bold)
SetGadgetFont(0,FontID())
SetGadgetFont(1,FontID())
SetGadgetFont(2,FontID())
SetClock(0,0,1000,"%hh:%ii:%ss","%hh %ii %ss")
SetClock(1,2,2000,"%hhh%iim%sss","")
SetClock(2,4,3000,"%hhh%iim%sss","")
Repeat
Until WaitWindowEvent() = #PB_Event_CloseWindow
ForEach Clock()
Debug Clock()\threadId
Next