B4J Question Timers vs Sleep

mindful

Active Member
Licensed User
Can someone point the differences (performance, memory) between using a timer or using the new sleep feature inatead?

With the new sleep feature we can simulate a timer like:
B4X:
Sub SleepTimer()
  Do While True
    ... some code
    Sleep(1000)
  End While
End Sub

And we can 'activate' the simulated timer just by calling the SleepTimer sub.

So i was wondering what are the differences 'behind the scene' and if there are any advantages or disadvantages of using sleep vs timers.

Regards,
 

mindful

Active Member
Licensed User
I've wrote that code as a small example of how I am using it. I am using it in WebSocket class and I am assigning private variable instead of True.

B4X:
Private FirstActionEnabled as Boolean

Sub RunFirstAction()
  FirstActionEnabled = True
  Do While FirstActionEnabled
    ' get some data from websocket
    ' post some data to another external server
    Sleep(5000)
    ' get some data from the external server
  End While
End Sub

Sub StopFirstAction()
  FirstActionEnabled = False
End Sub

I have about 9 of this 'action' subs for each websocket. 4 of them I start right after WebSocket_Connected and run for as long as the WebSocket is Open and 5 of them I start and stop if some code condition is met when I get data from client (browser) via websocket.

My project isn't finished yet. I am collecting data from other applications(point of sale applications), analizing it and update the db of my project and the client(browser) view. Basically all the external servers are local vb6 applications that produce some logs now and then and I am centralizing that data so I and the users using those applications can see their data in a modern way (i am using abmaterial for the gui of my project).

From my point of view is simple to use the Sleep method and the code is more 'clean' instead of Timers but what I really don't get is what happens to the loop when Sleep is called :) is it left hanging ? It exits the loop and after sleep is finished it recreates the loop?
 
Upvote 0
Top