Timing a thread

Just starting out? Need help? Post your questions and find answers here.
TimmyTom
User
User
Posts: 36
Joined: Mon Aug 18, 2003 8:32 am

Timing a thread

Post 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
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post 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
-Beach
TimmyTom
User
User
Posts: 36
Joined: Mon Aug 18, 2003 8:32 am

Post by TimmyTom »

I can't believe i didn't think of that.. :oops:

Thanks dude.

Timmy, the idiot
Beach
Enthusiast
Enthusiast
Posts: 677
Joined: Mon Feb 02, 2004 3:16 am
Location: Beyond the sun...

Post 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.
-Beach
Post Reply