Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Developer
Posts: 204
Registered: ‎07-09-2009
My Device: Not Specified

How to schedule a Timer from within a TimerTask run() -- getting IllegalStateException

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:

 

        Timer2.schedule(TimerTask2, 40);

 

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:

 

         Timer2.cancel();

         Timer2.schedule(TimerTask2, 40);

 

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:

 

 

         if (timer2pend)

            Timer2.cancel();

         Timer2.schedule(TimerTask2, 40);

         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?

 

Thanks,

Mike

 

 

 

Developer
Posts: 16,999
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to schedule a Timer from within a TimerTask run() -- getting IllegalStateException

don't cancel the timer, cancel the timertask.
if you cancel the timer it is gone, you have to create a new one (and all scheduled tasks are no longer present, of course)
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter