Wait for Timer??

enonod

Well-Known Member
Licensed User
Longtime User
I needed to call the sub that triggers the timer from two different places and circumstances. (pseudo code below)
The first is a Touch and so will be awaiting an event. OK
The second is in a recursive system and needs code following the timer triggering.

It did NOT work and analysis showed that it was 'probably' because once the timer was enabled the following code continued, before the timer triggered to run its own code. Thus the following code ran before the timer code, out of proper sequence.

I solved this by adding the following in Sub B...
Do While Timer.enabled
DoEvents
Loop

It does let the code run in correct sequence and the result works, but I am concerned that it may be 'cheating' and something will go wrong at some point as yet untested.

Any confirmation that this is 'legal' and that no problems should arise will be most gratefully received. Any suggestions that it is wrong, will be received :)

B4X:
Sub pnl_Touch(...) 
   C    'Triggers timer which performs some slow actions by repeated ticks
End Sub

Sub B  'THIS IS RECURSIVE
   C         'When this starts the timer, the code...
      Do While Timer.enabled     'Added to wait for Timer??
         DoEvents
      Loop
   more stuff      '...continued here because the timer
End Sub         '   has not yet triggered.

Sub C
   ...
   If ... Then
   count=20
   Timer.enabled=True
   End If
   ...
End Sub

Sub Timer_Tick      'one of 20 cycles
   If count>0 Then
      count=count-1
      some action
   Else
      Timer.enabled=False
      ...
      B         'this will retrigger timer via C
      ...
   End If
End Sub
 

thedesolatesoul

Expert
Licensed User
Longtime User
It looks legal and it *may* work on most versions of android. But it highly depends on whether DoEvents will allow the Timer message to go through.
Also, it is not the most efficient processor-wise, it is confusing program flow, and a pain to debug.
If I were you I would split the subs (B), add a call back from the timer.
EDIT:
Erel answered before me!
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thank you both.
You should instead move "more stuff" to a sub that is called from Timer_Tick.
If I were you I would split the subs (B), add a call back from the timer.
Unfortunately I cannot move 'more stuff' because it consists of 'End If's' and 'Next' Statements. The call to enable the time is within those, allowing for recursion.

The timer code is to move objects 'slowly' while doing recursive tests but I cannot put all the code in the timer Tick because it would slow down the tests also.

Does this really mean a redesign despite it 'appearing' to work.
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Having accepted that I need a different approach, is there any way to slow down a For/Next or Do While loop. I am trying to move an object by coordinates and test it each move, such that the movement is visible?

Without the timer loop (which is within the recursive test loop) to slow the moves the object simply appears at the destination, which is not what I want. I want to see it move like a sprite.
The tests at each move must be recursive or I would need to build a stack for the many possible outcomes and coordinates with regard to other objects.
The tests are working fine.
Thanks in advance for any suggestions that I have missed
 
Upvote 0

stevel05

Expert
Licensed User
Longtime User
This is something that could also be done with a timer.

Perhaps a separate timer? Without knowing your code it's difficult to tell.
via Tapatalk
 
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
The pseudo code is in essence as posted. An example of a similar use would be the game of asteroids.
If you imagine a loop that moves the asteroid a step at a time and each step a test must be carried out for collision. That is straightforward.
If when the collision occurs several fragments start moving or are released in different directions, they also move a step at a time and further tests are required for each without losing track of the first coords etc.
The number of tests are unknown because the number of fragments is unknown etc. etc.
Therefore each collision leading to other collisions leads to the need for recursive tests so that the subroutine stack keeps record of the coordinates of each fragment for an unpredictable depth.
My difficulty came because the movement of each object (within a timer tick sub to slow it) is within the recursive loop and the code continues after the timer is enabled as posted above. I solved it but the posts above caused my current question.
My conclusion, ditch using a timer to slow the movement steps.
I also understand that slowing a do while etc. with a delay loop to slow the movement is also taboo because it locks the system while the delay runs.
Without the timer system the object simply arrives taking the user aback.

Hence my question... is there a way of achieving that some other way?
[EDIT]Erel, if you look at this again, it was the screen update of movement I wanted DoEvents for.
 
Last edited:
Upvote 0

stevel05

Expert
Licensed User
Longtime User
Apart from the issue of upsetting the OS by using a loop, you would be restricting your ability to process things in parallel, as the loop will run until it's finished. Using a timer you can do small parts of many jobs which is far more convienient.

I am not a games programmer, so probably can't give you the solution. However, I believe you need to look at the design of the app in the light of new information.

via Tapatalk
 
Last edited:
Upvote 0

enonod

Well-Known Member
Licensed User
Longtime User
Thank you stevel05. I just got a stack overflow, which is enough to cause a rethink.
 
Upvote 0
Top