Threads and multiple parameters....

Just starting out? Need help? Post your questions and find answers here.
roachofdeath
User
User
Posts: 78
Joined: Sun Apr 24, 2005 3:22 am

Threads and multiple parameters....

Post by roachofdeath »

Threads and multiple parameters.... how?
Paul Dwyer
User
User
Posts: 44
Joined: Wed Nov 05, 2003 4:34 am
Location: Tokyo, Japan

Post by Paul Dwyer »

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.
Paul Dwyer
Network Engineer
Aussie in Tokyo
localmotion34
Enthusiast
Enthusiast
Posts: 665
Joined: Fri Sep 12, 2003 10:40 pm
Location: Tallahassee, Florida

Post by localmotion34 »

the little known, seldom used function of POSTMESSAGE_()

and PEEKMESSAGE_(). that way, its possible to have threads create, access, and alter other threads.

Code: Select all

!.WHILE status != dwPassedOut
! Invoke AllocateDrink, dwBeerAmount
!MOV Mug, Beer
!Invoke Drink, Mug, dwBeerAmount
!.endw
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

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.
i come here with a small example on how to 'pass'
multiple arguments to a threaded procedure.

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
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
dell_jockey
Enthusiast
Enthusiast
Posts: 767
Joined: Sat Jan 24, 2004 6:56 pm

Post by dell_jockey »

thanks Flype,

a very nice demo, without any unneccesary ballast. Very useful to me. Thanks again!
cheers,
dell_jockey
________
http://blog.forex-trading-ideas.com
User avatar
Flype
Addict
Addict
Posts: 1542
Joined: Tue Jul 22, 2003 5:02 pm
Location: In a long distant galaxy

Post by Flype »

thanks, dell_jockey.

and as a second example,
it's possible to 'encapsulate' the creation of a threaded clock in a classic procedure...

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
note:
i make use a linked list ( i could use an array ) because the structured variables must be 'global' to be still 'alive' from/inside the thread.
No programming language is perfect. There is not even a single best language.
There are only languages well suited or perhaps poorly suited for particular purposes. Herbert Mayer
Post Reply