I use a scheduler to ensure that my loop does not take too long, so I exit the loop if the scheduler fires and ends the loop.
This is quite straight forward:
public void startSchedulerForTimeout() {
Log.i("x", "SCHEDULING TIMEOUT " );
timeoutForLoopReached = false;
scheduler.schedule(new Runnable() {
public void run() {
timeoutForLoopReached = true;
Log.i("x", "SET TIMEOUT FLAG ");
}
}, 10, TimeUnit.SECONDS);
}
so in my code I just write:
startSchedulerForUSBMessageTimeout();
while (!done && !timeoutForLoopReached) {
...do the loop stuff
}
To make sure that the scheduled task does not fire when I run the loop again I need to unschedule the obsolete task (in case I ended the loop before timeout) so that the new timer will start again at the beginning of a new loop (instead of the old one firing after the first loop has been finished right in between the second time the loop executes)
How can I just unschedule any scheduled task that has not been fired to avoid this scheduled task mixup?
Many thanks!
Use ScheduledFuture: