Wish New Sleep feature

JohnC

Expert
Licensed User
Longtime User
As mentioned in this thread:

https://www.b4x.com/android/forum/t...r-asynchronous-programming.77867/#post-493180

"Sleep" is a new delay keyword that does not block the main app.

B4X:
Sleep(1000)

What would be real helpful is if it supported an additional/optional "abort" parameter:

B4X:
Sleep(msDelay as Int, ExitWhenTrue as Boolean)

This way we can include a Boolean variable when calling the routine that, when it's value becomes true, it will exit the sleep delay right away instead of waiting the full msDelay time:

B4X:
FTP.Download
Sleep (90000, DownloadCompleted)

This would allow us to response to certain external events right away and not have to wait for the sleep delay to complete.

There could also be a third optional "ExitWhenFalse" parameter for instances when you want to exit the sleep delay when a [global] variable turns false.

B4X:
Sleep(msDelay as Int, ExitWhenTrue as Boolean, ExitWhenFalse as boolean)
 
Last edited:

DonManfred

Expert
Licensed User
Longtime User
For what you want to do the WaitFor method can be used i guess (based on the code erel posted). No need for a sleep
 

JohnC

Expert
Licensed User
Longtime User
I don't see exactly what "WaitFor" does.

But, my suggestion allows for a dual-purpose "sleep" feature: 1) Delay Only or 2) Delay, but abort delay early if a condition is met.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
This would allow us to response to certain external events right away and not have to wait for the sleep delay to complete.
I think that you misunderstood how the Sleep feature works. It doesn't pause the main thread. It pauses the sub and resumes it after the specified period. Other events can be raised at that time.
 

JohnC

Expert
Licensed User
Longtime User
I think that you misunderstood how the Sleep feature works. It doesn't pause the main thread. It pauses the sub and resumes it after the specified period. Other events can be raised at that time.

I do understand that.

But many times I run into situations where my app is waiting (doing a sleep), but then something external to the routine happens and I need to execute the code immediately after the sleep function right away without having to wait for the full sleep duration to elapse before the code is executed.
 

JohnC

Expert
Licensed User
Longtime User
In VB6 I created a routine called "Wait" that came in very handy:

B4X:
Public Sub Wait(Delay As Variant, Optional ByRef AbortWhenVarTrue As Boolean = False, Optional ByRef AbortWhenVarFalse As Boolean = True)

    Dim EndTime   As Double
    Dim StartTime As Double
    Dim D         As Single

    D = CSng(Delay)
    StartTime = Timer

    EndTime = StartTime + D

    Do While Timer < EndTime
        DoEvents

        If Timer < StartTime Then

            If EndTime > 86398 Then
                EndTime = D - (86398 - StartTime)
            End If

        End If

        If AbortWhenVarTrue = True Or AbortWhenVarFalse = False Then
            Exit Do
        End If

    Loop

End Sub

The above routine was non-blocking (as your new Sleep is), and it would abort the delay/waiting time if a variable I passed to it changes.

This way the code after the Wait function can run right away if a condition occurs and helps keep things in a synchronous style.
 

JohnC

Expert
Licensed User
Longtime User
Basically, there will be times when you will *always* want the Sleep duration to fully complete. But then there will be times that it would be helpful if the delay can be aborted and allow the code immediately following the "Sleep" line to execute immediately. My suggestion simply allows an external event to abort the sleep delay when needed.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The above routine was non-blocking (as your new Sleep is)
DoEvents is the opposite of the new sleep feature. It holds the main thread in a loop (while processing the message loop). The new sleep feature is much more sophisticated and it doesn't hold the main thread. It instead stores the current context and later resumes it.

Anyway, As DonManfred wrote the situation you described is better suited for the new Wait For construct:
B4X:
Sub DownlodSomething
Dim j As HttpJob
j.Initialize("j", Me)
j.Download("http://www.google.com")
Wait For JobDone(j As HttpJob)
If j.Success Then
   Log(j.GetString)
End If
j.Release
 

JohnC

Expert
Licensed User
Longtime User
But how would you set a maximum time to wait using "Wait For"? (for the situations when you are not using an internet services which has a built-in timeout function)
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
One of the nice things about this feature is that nothing bad will happen if the event is never raised. The stored sub (which is lightweight) will eventually be removed.
The program is not waiting for the event. It just knows that it needs to route a future event to this sub instead of the standard event handler.

Timeouts should be implemented in the library that raises the event.
 

JohnC

Expert
Licensed User
Longtime User
I guess I am just not understanding exactly what this new "Sleep" function does/used for.
 

Jhonn

Member
Great!

Works this way?, call for example a Job, while the application continued running and when Job complete back to the next line where "wait for" was write??
 
Last edited:
Top