B4J Tutorial [B4X] Resumable Subs - Sleep / Wait For

Discussion in 'B4J Tutorials' started by Erel, Apr 19, 2017.

Thread Status:
Not open for further replies.
  1. ByteCounter

    ByteCounter Member Licensed User

    How do you deal with resumable subs inability to return values?

    I have a sub that benefits from using Sleep() as a timeout delay (while waiting on Astreams NewData event which sets a DataReceived Flag if data was received). The Sleep command pauses the resumable sub, allows other message events to be processed (i.e. NewData) and eventually the Sleep finishes and the resumable sub continues execution after the Sleep() command.
    After the Sleep delay I check the DataReceived Flag and I would like to return a boolean true/false flag to the resumable sub caller.

    I could use a global flag which is checked after calling the resumable sub but I'm just wondering if there is a better way that I haven't thought of.

    Note that I understand why the resumable sub can't return data to the caller ("Whenever Sleep or Wait For are called, the current sub is paused. This is equivalent to Return" and "Calling Sleep or Wait For is equivalent to calling Return from the caller sub perspective."

    How to return data to resumable sub caller?
     
  2. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed User

    with this you can return a value to the calling sub:

    Code:
    private sub log1 
    log2 
    wait for log2_completed(r as string)
    log(r)
    end sub

    private sub log2 
    dim r as string = "Hello"
    sleep(
    1000)
    callsubdelayed2 (me,log2_completed,r)
    end sub
     
  3. Erel

    Erel Administrator Staff Member Licensed User

  4. LucaMs

    LucaMs Expert Licensed User

    I fear that you cannot replace DoEvents with Sleep(0) to allow refreshing the GUI because Sleep, even if the time is set to 0, returns control to calling routines.

    So, I don't see other good alternative to DoEvents.
     
  5. Erel

    Erel Administrator Staff Member Licensed User

    This is not the correct place to start a discussion. Sleep is a good alternative to DoEvents. You should of course understand how it works. If you want to discuss anything then please start a new thread in the questions forum.
     
  6. LucaMs

    LucaMs Expert Licensed User

  7. devpialkt

    devpialkt Member Licensed User

    is "wait for" ready for websocket client ?
     
  8. Erel

    Erel Administrator Staff Member Licensed User

    Yes.
     
  9. devpialkt

    devpialkt Member Licensed User

    any sample or link code to check is connected, refused, or disconnected with "Resumable Wait For"?

     
  10. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread in the questions forum.
     
  11. devpialkt

    devpialkt Member Licensed User

  12. rossati

    rossati Active Member Licensed User

    Wait for is a good enhancement my be the next step is to return data?
     
  13. Enrique Gonzalez R

    Enrique Gonzalez R Well-Known Member Licensed User

    as explained in the post #42,

    wait for is completly capable of returning data.
     
    rossati and Erel like this.
  14. Erel

    Erel Administrator Staff Member Licensed User

  15. FabioG

    FabioG Active Member Licensed User

    Hello,

    I have a problem

    I have this part of code

    Code:
    Sub Start

    ProgressDialogShow2("Wait Please",True)

    ....other code....

    Wait(500)

    ...bla bla bla

    End Sub


    Sub Wait(MilliSecondi As Int)
       
    Dim Ti As Long
       Ti = 
    DateTime.Now + (MilliSecondi)
       
       
    Do While DateTime.Now < Ti
          
    DoEvents
        
    Loop
       
    End Sub

    If I remove DoEvents, ProgressDialog does not appear
    I tried to replace DoEvents with Sleep(0) but it does not work



    Any tips?
     
  16. Erel

    Erel Administrator Staff Member Licensed User

    You should delete the Wait sub and replace it with Sleep(500).
    For further discussion please start a new thread in B4A forum.
     
  17. FabioG

    FabioG Active Member Licensed User

    Thanks
     
  18. luciano deri

    luciano deri Active Member Licensed User

    Is there something of similar for B4A?
     
  19. Erel

    Erel Administrator Staff Member Licensed User

    Please start a new thread in B4A questions forum.
     
  20. moore_it

    moore_it Active Member Licensed User

    how i do for kill an resumable sub from UI ?
     
Thread Status:
Not open for further replies.
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice