Android Question How to "Pause" a thread

mscientist33

Active Member
Licensed User
I have a B4XTable. The table has two columns, "String command" and "Delay". The string command contains what will be written to a label and the Delay on the same line is the countdown until it advances to the next row.

I have a "Play" button and a "Pause" button and a "Stop" button. When I hit play, it should put the string command from the first row into the label and start counting down the delay in the delay cell. Once it reaches 0, the next row is programmatically selected, the next string command is put into the label and the new delay starts counting down.

All this is fine, but I don't have a way to "Pause". Currently I have it working with just a for loop doing the countdown but I don't know of a way to pause it. If I use a thread is it possible to "Pause" the thread? and unpause to pick up where it left off? I'm not fully understanding threads and I know it has a start and stop and an interrupt, but no pause like in windows threads. Is there a better way than using threads? a way to actually manually "pause" a sub?
 

JohnC

Expert
Licensed User
Longtime User
Instead of using a for/loop, use a timer and set the "interval" to "1000" milliseconds (1 second), and while the timer is running, it will call the event sub named "tmrMain_Tick()" every second. And in this sub you would decrement the count, and when it hits zero, handle it like you do now.

When you want to start or "resume" the countdown timer:
B4X:
tmrMain.Enabled = True

Then when the user clicks "Pause", just do:
B4X:
tmrMain.Enabled = False

When the timer is disabled, it wont call the trmMain_Tick() sub - so the countdown pauses while the timer is not running.
 
Last edited:
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…