Android Question Abort sleep()?

clydebamford

Member
Licensed User
is there a way to break out of a sleep() before it wakes up so that if something unrelated happens and we no longer want to run the code after sleep we can abort? I've been using a global var and checking that before continue with the code after sleep and I guess thats probably simpler than tring to identify which sleep call to abort!
 

clydebamford

Member
Licensed User
I'm using exo to play small pieces of a large mp4 on demand and inbetween clips I play another 10 sec clip on a loop using sleep() to loop the clip. It seems to get confused when I try to jump to another frame in another sub and then sleep finishes and tries to pause the video and loop...so basically I'm trying to controll exoplayer from 2 different subs and the sub that has a long sleep takes over when it wakes up, ?
 
Upvote 0

Erel

Administrator
Staff member
Licensed User
I think that you should use a global variable for this case.

Create a global TaskIndex int variable and use it like this:
B4X:
Sub ThisIsTheResumableSub
 TaskIndex = TaskIndex + 1
 Dim MyTask As Int = TaskIndex
 '...
 Sleep(...)
 If MyTask <> TaskIndex Then Return

Canceling the current waiting sub is simple as writing: TaskIndex = TaskIndex + 1
You can see this pattern in Camera2 class which is completely asynchronous.
 
Upvote 0

clydebamford

Member
Licensed User
nice!, thanks for that. I was using a booleon var but I guess your way will avoid problems with multiple reuseable subs, cheers!
 
Last edited:
Upvote 0
Top