07-16-2009 05:45 AM
Say I have Timer1 that triggers TimerTask1, and inside the run() method of that timertask, I find that I need to schedule something else to trigger once 40 ms from now, while Timer1 continues triggering at its own rate. So, inside the run() of TimerTask1, I do this:
The first time, it succeeds. The second time, it barfs up IllegalStateException "Already scheduled or cancelled".
So, I figured since this is a slow device maybe Timer1 triggered again before Timer2 did, even though it should have happened the other way around. But there are software timers in a non-real-time system that can go awry. So I tried this:
This time, it barfs up IllegalStateException "Timer already cancelled."
It throws this on the schedule(), not the cancel().
?? Say, what? The timer is cancelled, yes, I just did that, thank you. Now I'm trying to reschedule it, so why is it a problem that it was cancelled?
I never did a TimerTask2.cancel(), which would have permanently killed that task if I understand the system right.
I'm cancelling and rescheduling Timer1 as I please and THAT works, so what's up with Timer2?
Next I made sure that schedule() is never called while the timer is already scheduled but didn't execute yet, and that cancel() is never called when the timer is not scheduled. This was simple:
timer2pend = true;
then, in Timer2 run():
.... do what needs to be done...
timer2pend = false; // the timer has now executed and can be rescheduled without being cancelled first
I still got the "Timer is already scheduled" on the 2nd schedule() attempt.
I can only think that whatever the message string, the real problem may be that I'm trying to schedule a timer while in a timer event.
Anyone else experienced this?
07-16-2009 05:57 AM