Stop and restart timer task in Android
Exactly as you did.
To restart a Timer with a different timing, you need to cancel() the old timer and create a new one.
Your code probably is not working just because you are hiding the member variable timerRealData with a local variable with the same name.
Try to change:
Timer timerRealData = new Timer();
with
timerRealData = new Timer();
and it should work.
How to reset a timer in C#?
I always do ...
myTimer.Stop();
myTimer.Start();
... is that a hack? :)
Per comment, on Threading.Timer, it's the Change method ...
dueTime Type:
System.Int32
The
amount of time to delay before the
invoking the callback method specified
when the Timer was constructed, in
milliseconds. SpecifyTimeout.Infinite
to prevent the
timer from restarting. Specify zero
(0) to restart the timer immediately.
How do I stop and reset a setInterval timer
What's happening now is that your variable keeps going up, even when you "reset" the timer.
Simply add
totalSeconds = 0;
to your stopTimer
function. This will reset the variable to zero.
Does System.Timers.Timer.Stop() restart the interval countdown?
Short question, short answer. Your timer will start with a fresh interval of 1000ms after it has been stopped. See also MSDN: System.Timers.Timer.Stop()
Pause the timer and then continue it
If you have already canceled one timer, you can't re-start it, you'll have to create a new one.
See this answer, it contains a video and the source code how I did something similar.
Basically there are two method: pause and resume
In pause:
public void pause() {
this.timer.cancel();
}
In resume:
public void resume() {
this.timer = new Timer();
this.timer.schedule( aTask, 0, 1000 );
}
That makes the perception of pause/resume.
If your timers perform different actions based on the state of the application you may consider use the StatePattern
Fist define a abstract state:
abstract class TaskState {
public void run();
public TaskState next();
}
And provide as many states as you like. The key is that one state leads you to another.
class InitialState extends TaskState {
public void run() {
System.out.println( "starting...");
}
public TaskState next() {
return new FinalState();
}
}
class FinalState extends TaskState {
public void run() {
System.out.println("Finishing...");
}
public TaskState next(){
return new InitialState();
}
}
And then you change the state in your timer.
Timer timer = new Timer();
TaskState state = new InitialState();
timer.schedule( new TimerTask() {
public void run() {
this.state.run();
if( shouldChangeState() ) {
this.state = this.state.next();
}
}
}, 0, 1000 );
Finally, if what you need is to perform the same thing, but at different rates, you may consider using the TimingFramework. It is a bit more complex but let's you do cool animations, by allowing the painting of certain component take place at different rates ( instead of being linear )
How do I correctly restart a java.util.Timer?
you have to assign a new instance of a Timer. for example, see below:
import java.util.Timer;
import java.util.TimerTask;
public class Main {
private static Timer timer;
public static void main(String[] args) throws InterruptedException {
Main main = new Main();
main.launchSomeTimer("john doe");
Thread.sleep(5000);
main.resetSomeTimer();
Thread.sleep(10000);
timer.cancel();
}
private void launchSomeTimer(String name) {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
System.out.println("name: " + name);
}
};
timer = new Timer();
timer.schedule(timerTask, 500);
}
public void resetSomeTimer() {
TimerTask timerTask = new TimerTask() {
@Override
public void run() {
System.out.println("updating timer");
}
};
timer.cancel();
timer = new Timer();
timer.schedule(timerTask, 1000);
}
}
Related Topics
Using Environmentobject in Watchos
What Language Is Swift Written In
How to Implement Hash(Into:) from Hashvalue in Swift
Get Playground to Display All Loop Results
How to Prevent Actor Reentrancy Resulting in Duplicative Requests
How to Implement a Spritekit Timer
Adding Activity Indicator to Uialertview
Healthkit Error: Missing Com.Apple.Developer.Healthkit Entitlement
Swift Uisearchcontroller Wired Up in Core Data Project, App Runs, But Search Not Updating
Mutate Function Parameters in Swift
Show/Hide Password - How to Add This Feature
Nsuserdefaults in Swift - Implementing Type Safety
How to Avoid Using Anypublisher/Erasetoanypublisher All Over the Place