• 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

Timer IllegalStateException

 
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Look at this code


I'm receiving IllegalStateException, timer is already canceled when I call stopTimer().
But this is a try {} catch {} ... why isn't it working ?
I even tried adding a boolean to check if timer is canceled or no, and I still get same result. It's always crashing..
Anyone knows any solution for this? And why the try statement not working ?
Thanks For your help.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The cancel() method doesn't throw an IllegalStateException. It is the scheduleXXX() methods that throws them. Are you sure you are looking in the right place?

Henry
 
Sam Benry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I didn't know that. Thanks a lot.
This changes the problem completely. What I am trying to do is enable and disable the timer at different places. Here is an example:
The program runs timer.schedule(); will run
Some info is received timer.cancel(); will run
Some info is received timer.schedule(); will run to start the timer again.
etc...

After I stop the timer the first time, I can't start it again ? What should I do? I need to start it and cancel it as much times as requested..
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't cancel the Timer but cancel the TimerTask instead.
 
Sam Benry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how to do this ?


I should cancel RemindSignInTask only ? how ?
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
How about calling its cancel() method?

You will need to keep a reference to the task of course.
[ September 25, 2008: Message edited by: Rob Prime ]
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am not convinced if either technique is best -- especially if you have to constantly turn the timer on and off.

If you cancel the timer, then you need to create a new timer, and reload all of the tasks, to turn it back on.

If you cancel each task, you won't have to create a new timer, but you still need to reload all of the tasks, to turn it back on.

How about making it cooperative? Have each of the tasks check a flag. If the flag states that it is off, then the task will skip the cycle. This way, turning the feature on or off, is as simple as setting a flag.

Henry
 
Rob Spoor
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That has one drawback - you're stuck to the existing schedule. If you want to shift the schedule by a few seconds (a.k.a. restart it starting from now), or have a new schedule, you will have no other choice but to cancel and reschedule. If it only matters that the task occurs regularly with a fixed interval, then skipping is a good solution.
 
Bartender
Posts: 4179
22
IntelliJ IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Rob Prime:
That has one drawback - you're stuck to the existing schedule. If you want to shift the schedule by a few seconds (a.k.a. restart it starting from now), or have a new schedule, you will have no other choice but to cancel and reschedule. If it only matters that the task occurs regularly with a fixed interval, then skipping is a good solution.




I guess we are drifting a bit from the OP's question, but would having each task be scheduled as a single-execution task that re-schedules itself at the end of the run method solve this? Instead of having the Timer control the delay, you would have a stored or calculated delay inside the TimerTask. At the end of the run you timer.schedule(this,delay) for the next occurence.

Which gives you the flexibility to use a flag to skip a cycle, an instance variable to modify schedules, and another flag to prevent rescheduling.
 
reply
    Bookmark Topic Watch Topic
  • New Topic