Page 1 of 1

Timing a thread

Posted: Wed Feb 23, 2005 3:13 am
by TimmyTom
I need to execute stuff in a seperate thread about very two seconds, instead of having it repeat over and over without pause.

i tried with ElapsedMillisecs() and this works, until one goes to a different program and come back, then it has magically stopped timing which makes my thread not do anything anymore (which is not what i want).

Code: Select all

Declare threadthingamabob(whatever)

win = OpenWindow(1,100,100,200,200,0,"hmm")

thread = CreateThread(@threadthingamabob(),0)

Repeat
  Eventid.l = WaitWindowEvent()
  
  If EventID = #PB_Event_CloseWindow  ; If the user has pressed on the close button
    Quit = 1
  EndIf
Until Quit = 1

Procedure threadthingamabob(whatever)
  Start = 0

  Repeat
    If Start = 0
      Start = ElapsedMilliseconds()  
    EndIf
    
    If ElapsedMilliseconds() - Start => 1999
      Debug "THREAD EXECUTE"
      ; do stuff
      
      Start = 0
    EndIf
  ForEver
EndProcedure
So, the above code WORKS, until the app looses focus then somehow the thread doesn't work anymore. I need this to work no matter if the app has focus or not (and definitely it should work if the app gains focus again)

Any ideas?

Tim

Posted: Wed Feb 23, 2005 4:11 am
by Beach
Why not use delay instead? This works for me

Code: Select all

Procedure threadthingamabob(whatever)
  Repeat 
    ; do stuff
    Debug "THREAD EXECUTE"
    Delay(2000)
  ForEver
EndProcedure 

win = OpenWindow(1,100,100,200,200,0,"hmm")

thread = CreateThread(@threadthingamabob(),0)

Repeat
  EventID.l = WaitWindowEvent()
  
  If EventID = #PB_Event_CloseWindow  ; If the user has pressed on the close button
    Quit = 1
  EndIf
Until Quit = 1

Posted: Wed Feb 23, 2005 4:32 am
by TimmyTom
I can't believe i didn't think of that.. :oops:

Thanks dude.

Timmy, the idiot

Posted: Wed Feb 23, 2005 5:05 am
by Beach
Don't sweat it... :wink: I might use that 'ElapsedMilliseconds()' command for someting I was working on... so it was a good thing after all.