Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Timer IllegalStateException

 
Sam Benry
Ranch Hand
Posts: 89
  • Mark post as helpful
  • send pies
  • 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.
 
Henry Wong
author
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
  • 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..
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
how to do this ?


I should cancel RemindSignInTask only ? how ?
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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
Marshal
Pie
Posts: 20894
76
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • 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.
 
Steve Luke
Bartender
Posts: 4181
21
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • 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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic