Results 1 to 3 of 3
- 12-31-2011, 03:34 AM #1
Member
- Join Date
- Nov 2010
- Posts
- 11
- Rep Power
- 0
run a method with timer that only runs when nothing is happening
Hi guys im looking or anyone that can point in the right direction when it come to timers
i have a method that closes one window and opens another.
but im looking to make it so this methods only runs after a certain amount of time has passed
and this times only starts when nothing is happening
for example, i want this method to run after 1 minute.
so i press a button, then this timer starts because nothing is happening but if another button is pressed the timer is restarted but if nothing is pressed for that whole minute it runs the afore mentioned method.
Thank you for any info
- 12-31-2011, 04:43 AM #2
Senior Member
- Join Date
- Jul 2011
- Location
- Melbourne, Victoria, Australia
- Posts
- 155
- Rep Power
- 2
Re: run a method with timer that only runs when nothing is happening
Have you looked into using System.currentTimeMillis();
You could record the start time and once every pass of a loop have another variable(timePassed) that is equal to the current time minus the start time.
Then check that timePassed was not more than 1 minute's worth of milliseconds.
-
Re: run a method with timer that only runs when nothing is happening
Keep a class field for the last time the user was active
private long lastActive;
Create a setter to update the field
private void setLastActive() { lastActive = Calendar.getInstance.getTimeInMillis(); }
Add the setter at the end of each action
public void ActionMethod() {
...
setLastActive();
}
Add a class field to define the timeout after which the user is deemed inactive
private final long inactivePeriod = 1*60*1000; //1min
Create a method to check for inactivity
private boolean isActive() {
return (lastActive-Calendar.getInstance().getTimeInMillis() >= inactivePeriod);
}
Create a method to deal with inactivity
private void ifInactiveStart() {
if (!isActive()) {
//create a timer
...
}
}
Listen for inactivity.... this could be done in any method called regularly but probably best done whenever the value is changed, so we alter the setter
private void setLastActive() {
...
ifInactiveStart();
}
Create a timer task
Timer t = new Timer(); //java.util.Timer
t.schedule(new TimerTask() {
public void run() { ... }
}, inactiveTimerDelay);
Similar Threads
-
Multicast receive not happening in multiple interface or multi NIC
By Vishwanath in forum NetworkingReplies: 1Last Post: 06-07-2011, 07:22 AM -
I don't know what is happening
By Fred1 in forum New To JavaReplies: 2Last Post: 04-27-2011, 11:21 PM -
Stopping a Timer from Inside the timer
By krishnan in forum Java AppletsReplies: 2Last Post: 10-04-2010, 11:15 PM -
countdown timer, little help with method
By sidy in forum New To JavaReplies: 22Last Post: 07-19-2008, 12:42 PM -
How to cancel an individual timer in spite of canceling whole timer
By Java Tip in forum java.utilReplies: 0Last Post: 04-04-2008, 02:46 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks