aspose file tools*
The moose likes Java in General and the fly likes Help needed with java.util.Timer Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Help needed with java.util.Timer" Watch "Help needed with java.util.Timer" New topic
Author

Help needed with java.util.Timer

Yousef Dardeer
Greenhorn

Joined: Nov 01, 2005
Posts: 3
Hey guys. I�m trying to use a timer (package java.util.Timer and java.util.TimerTask) and I want the timer to be triggered from a button. Not just from the method �schedule� .

Now here is a program which I was trying to execute but it keeps giving me errors... (IllegalStateException)

/***************************/

/********************************************/

Now whenever I try to click on the button(whether the timer is running or after it has ran out), the NetBeans keeps giving me that error (Exception in thread "AWT-EventQueue-0" java.lang.IllegalStateException: Timer already cancelled.). So appearantly the error is either in the 'actionPerformed' or 'run' methods.
Then i read in another post that i have to use a new timer since i cannot restart a canceled timer. I tried that and it doesn't work.

Any help would be appreciated.
[ November 02, 2005: Message edited by: Yousef Dardeer ]
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Read the API:

Timer.cancel()
Terminates this timer, discarding any currently scheduled tasks. Does not interfere with a currently executing task (if it exists). Once a timer has been terminated, its execution thread terminates gracefully, and no more tasks may be scheduled on it.


You can't schedule a new TimerTask to run on a Timer that's been stopped/canceled. You should cancel the TimerTask using rt.cancel() instead.
Yousef Dardeer
Greenhorn

Joined: Nov 01, 2005
Posts: 3
Thanks for your response.

Using rt.cancel() realy cured the Exception problem. But now there is another one : (Please bear with me)

When i first run the program, it works and a timer is triggered. Then i click on button and the running timer stops and a new one is fired (just like it should). then when i press the button again, a new timer is fired. But the old one is not termenated. so i have two timers running until the old one runout.

so now my actionPerformed is like this :
Yousef Dardeer
Greenhorn

Joined: Nov 01, 2005
Posts: 3
nevermind guys . i used java.swing.Timer instead and it worked perfectly..
so thanxs anyways.
Ken Blair
Ranch Hand

Joined: Jul 15, 2003
Posts: 1078
Originally posted by Yousef Dardeer:
Thanks for your response.

Using rt.cancel() realy cured the Exception problem. But now there is another one : (Please bear with me)

When i first run the program, it works and a timer is triggered. Then i click on button and the running timer stops and a new one is fired (just like it should). then when i press the button again, a new timer is fired. But the old one is not termenated. so i have two timers running until the old one runout.

so now my actionPerformed is like this :


Sorry for the late reply. You're calling rt.cancel(), which cancels the original task and create a new task. However, the new task is assigned to local variable rr NOT rt. The next time this method is called, you cancel rt, the original task, again when it's already been canceled instead of canceling the one that replaced it (which you no longer have a reference to). Change RemindTask rr = new Remind...; to rt = new Remind...; and you'll be fine.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Help needed with java.util.Timer
 
Similar Threads
Timer Trouble
Help me please ... Timer
Threads doubt
errorr in compling.. help
Set Timer