Android Question Timer in while loop ?

Devv

Active Member
Licensed User
Longtime User
hi
i want to do something like this

B4X:
while (WIFICONNECTED)

somecodehere
sleep 30 seconds
loop

i may know how to do it but i dont know the best way to do it without draining the battery or using the cpu to much.

and i may have problems with timer in a loop

any 1 can help ?
 

walterf25

Expert
Licensed User
Longtime User
you should use the Timer keyword in B4A, Example
B4X:
Dim timer1 as timer
timer1.Initialize("timer1", 30)


Sub timer1_Tick
'Anything inside this sub will be executed every 30 seconds
End Sub

You should really take a look at the B4A Beginners guide, also you can use the search bar to find examples.

Cheers,
Walter
 
Upvote 0

KMatle

Expert
Licensed User
Longtime User
@Devv: Always code that way that you minimize cpu usage (and other ressources). Keep in mind that there are a lot of other processes run on your device. Constructing a real loop would use 100% cpu and you can imagine what happens then.

That is the reason why modern operating systems handle everything by events. An event is f.e. "Button pressed" or "Timer click". In walters example the os checks the wifi state every x seconds (maybe you want to reduce it from 30 to a lower value) seconds depending on your intention.
 
Upvote 0

Devv

Active Member
Licensed User
Longtime User


thanks for example bro...
i thought the timer interval is measured in milliseconds not seconds

timer1.Initialize("timer1", 30)
so 30 will be much less than one second it will not be 30 seconds

a'm i right ?
 
Upvote 0

walterf25

Expert
Licensed User
Longtime User
Yepp. For 30 Seconds you must initialize the timer with 30000.
Yes, sorry I meant 30000 milliseconds = 30 seconds.

Cheers,
Walter
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…