By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,967 Members | 1,690 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,967 IT Pros & Developers. It's quick & easy.

Can't stop the Timer....!

P: n/a
Hey.. I'm having problems with stopping this timer from outside the
'runTimer()' class.. i have no problems calling 'cancel()' from inside
the run() method but I want to be able to stop the Timer when I press
a button (stopTimer()).

With the code I've got here, i get an error saying 'void cannot be
dereferenced' ... has anyone got any ideas as to what I'm doing wrong
or how i could stop the timer by pressing the button? Any help would
be appreciated.. i'm fairly new to java so if you can keep it in
relative laymens terms thanks! :)

public void runTimer(){

int delay = 0; //
int period = 3000; //

java.util.Timer timer = new java.util.Timer();

timer.scheduleAtFixedRate(new TimerTask() {

public void run() {
//code does stuff here
}
}, delay, period);
}
//when stopTimer button is pressed.. timer should stop
public void stopTimer() {
runTimer().timer.cancel();
}
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
ge******@hotmail.com (Geoff) wrote in news:18143a5b.0403310531.7b0efb72
@posting.google.com:
Hey.. I'm having problems with stopping this timer from outside the
'runTimer()' class.. i have no problems calling 'cancel()' from inside
the run() method but I want to be able to stop the Timer when I press
a button (stopTimer()).

With the code I've got here, i get an error saying 'void cannot be
dereferenced' ... has anyone got any ideas as to what I'm doing wrong
or how i could stop the timer by pressing the button? Any help would
be appreciated.. i'm fairly new to java so if you can keep it in
relative laymens terms thanks! :)

public void runTimer(){

int delay = 0; //
int period = 3000; //

java.util.Timer timer = new java.util.Timer();

timer.scheduleAtFixedRate(new TimerTask() {

public void run() {
//code does stuff here
}
}, delay, period);
}
//when stopTimer button is pressed.. timer should stop
public void stopTimer() {
runTimer().timer.cancel();
}


Why is your method stopTimer() referencing runTimer() ??
runTimer() returns void, just as the compiler told you, so how could it
have a method or field called timer ?

I think that you meant timer.cancel(), NOT runTimer().timer.cancel()

Good Luck!
--
Ian Shef 805/F6 * These are my personal opinions
Raytheon Company * and not those of my employer.
PO Box 11337 *
Tucson, AZ 85734-1337 *
Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.