Multiple threads sleep for x seconds ?
Posted: Thu Jan 25, 2018 6:48 pm
For example I want to have two threads, one that calls an procedure at each 2 seconds and another one that calls an procedure at each 6 seconds.
This code need to run forever, untill the program is closed.
What i got for now:
This code works fine, but i think is not right.
Also, i want to do all this without using Sleep, as sleep will pause whole program and not only the thread.
Thanks for you time
This code need to run forever, untill the program is closed.
What i got for now:
Code: Select all
EnableExplicit
Procedure Thread1()
Define hEvent.l
hEvent = CreateEvent_(#Null, #True, #False, #Null$)
If hEvent
WaitForSingleObject_(hEvent, 2000)
CloseHandle_(hEvent)
Debug "Task each 2 seconds"
Thread1()
EndIf
EndProcedure
Procedure Thread2()
Define hEvent.l
hEvent = CreateEvent_(#Null, #True, #False, #Null$)
If hEvent
WaitForSingleObject_(hEvent, 6000)
CloseHandle_(hEvent)
Debug "Task each 6 seconds"
Thread2()
EndIf
EndProcedure
Procedure Main()
Define hEvent.l
Dim Threads.l(1)
Threads(0) = CreateThread_(#Null, 0, @Thread1(), #Null, 0, #Null)
Threads(1) = CreateThread_(#Null, 0, @Thread2(), #Null, 0, #Null)
WaitForMultipleObjects_(2, @Threads(), #True, #INFINITE)
CloseHandle_(Threads(0))
CloseHandle_(Threads(1))
EndProcedure
Main()
Also, i want to do all this without using Sleep, as sleep will pause whole program and not only the thread.
Thanks for you time