• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Help needed with java.util.Timer

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
nevermind guys . i used java.swing.Timer instead and it worked perfectly..
so thanxs anyways.
 
Ken Blair
Ranch Hand
Posts: 1078
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic