Android Question wait for usage

elitevenkat

Active Member
Licensed User
Longtime User
I am looping through a for next loop
B4X:
    for i =0 to 100
        blnok=false  ] declared globally 
        dim blngonext as boolen=false
        call reset
        wait for  (blnok) complete (blngonext )
        if blngonext  = true then
           ... some code
        endif
   next

private sub reset
   ...lot of commands executed here ..
    blnok=true
end sub

i am sure i am totally confused about the usage of wait for.
I am however using this 'wait for' in many projects for http job record fetching and processing.

all i want to do is, the for loop should be made to wait till blnok becomes true.
can anyone help ?
 

DonManfred

Expert
Licensed User
Longtime User
The forumsearch IS working:

Make your sub a resumeable sub.
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
It might be that you are still confused even after you have read the reference mentioned by DonManfred.
All I want to do is, the for loop should be made to wait till blnok becomes true.
To do what you say a Wait For is not required. The global variable "blnok" is enough to do the job, and I assume that is why you created it ...
B4X:
Private Sub example
    For i =0 To 100
      blnok  = False         ' declared globally
      reset
      Do Until blnok        ' this will loop until [reset] is finished and blnok = true
        Sleep(0)                ' this makes this a resumeable sub
      Loop
      If blngonext = True Then        ' ... some code
      End If
    Next
End Sub

Private Sub reset
    '  ...lot of commands executed here ..
    blnok = True
End Sub
You do not mention what the sub "reset" is doing. If it is solid computation then there is nothing to be gained by calling it from a resumeable sub, so presumably "reset" itself contains a "Wait For" or something that makes it resumeable. Also I don't know what "blngonext" is doing, so maybe I have missed something.
 
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
It might be that you are still confused even after you have read the reference mentioned by DonManfred.

To do what you say a Wait For is not required. The global variable "blnok" is enough to do the job, and I assume that is why you created it ...
B4X:
Private Sub example
    For i =0 To 100
      blnok  = False         ' declared globally
      reset
      Do Until blnok        ' this will loop until [reset] is finished and blnok = true
        Sleep(0)                ' this makes this a resumeable sub
      Loop
      If blngonext = True Then        ' ... some code
      End If
    Next
End Sub

Private Sub reset
    '  ...lot of commands executed here ..
    blnok = True
End Sub
You do not mention what the sub "reset" is doing. If it is solid computation then there is nothing to be gained by calling it from a resumeable sub, so presumably "reset" itself contains a "Wait For" or something that makes it resumeable. Also I don't know what "blngonext" is doing, so maybe I have missed something.
Even if your example worked (and it doesn't) the use is not the correct one the resumable sub was designed for

However it doesn't work, you call reset before the loop and the boolean variable is already true so it immediately exits the loop
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Even if your example works, the use is not the correct one the resumable sub was designed for
Well, it does work [see Edit below], but I agree that it is not what resumeable subs are really intended for. As I said in my comments, unless the "reset" subroutine is itself resumeable then the whole exercise is an unnecessary complication, and if "reset" is a resumeable sub then why not move it up a level and build a more conventional structure. But I feel that other things may be going on in other parts of this application that I know nothing about.

[Edit : You are right @Star-Dust , it doesn't work, at least not in any sensible way. But the variable "blnok" is there for a reason, and I still assume that "reset" must be a resumeable sub.]
 
Last edited:
Upvote 0

Star-Dust

Expert
Licensed User
Longtime User
If I understand what you want to do ....

Before continuing the cycle waits for the Next button to be pressed. When you want to stop, click the Exit Button.
B4X:
Dim ExitButton As Boolean = False
    For i =0 To 100
        Log($"Positione: ${i}"$)
        ' wait next button
        wait for blngonext_Click
        '   ... some code
       If ExitButton Then Exit
Next

private Sub blngonext_Click
   
End Sub

private Sub reset_Click
    '...lot of commands executed here ..
    ExitButton=True
    CallSub(Me,"blngonext_Click")
End Sub
 
Upvote 0

elitevenkat

Active Member
Licensed User
Longtime User
Thank you Star-Dust and Brian Dean. In fact i did some thing similar to the suggestion you made. i had put it in
B4X:
Do While Starter.nextok=False
           Sleep(0)
  Loop
worked fine.
 
Upvote 0
Top