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
precisionpete
Posts: 264
Registered: ‎10-05-2010
My Device: Not Specified
Accepted Solution

How to get a TimerTask to schedule itself?

I am trying to run a TimerTask at repeating irregular intervals.

 

i.e. When the TimerTask runs, it calculates the next interval, cancels itself, then schedules itself at the new interval.

 

Like this:

 

 

_runTimer = new TimerTask() {
	public void run() {
		_runTimer.cancel();
		int next = program[step];
		timer.schedule(_runTimer, next);
		step++;
	}

};

_runTimer.run()

Except this blows up with an IllegalStateException on the schedule line.

 

So if a TimerTask can not schedule itself, is there another pattern of how to do this?

 

Thanks

 

Please use plain text.
Developer
indusBULL
Posts: 207
Registered: ‎09-10-2010
My Device: Not Specified

Re: How to get a TimerTask to schedule itself?

 

Use Timer.scheduleAtFixedRate(timertask, delay. interval). Using timer class, you can schedule timertask at regular intervals. Google it for more information.

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: How to get a TimerTask to schedule itself?

Once you cancel the TimerTask, it is no longer usable - you'll have to create a different one and schedule that.

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: How to get a TimerTask to schedule itself?

[ Edited ]

If you want your TimerTask to run at irregular intervals you can create a custom class that extends it and create instances of it to schedule.

 

 

Timer timer = new Timer();

private class RunTimer extends TimerTask {
   public void run() {
      int next = program[step];
      timer.schedule(new RunTimer(), next);
      step++;
   }
}

_runTimer = new RunTimer();

timer.schedule( _runTimer(), program[step] );
step++;

 

 

Please use plain text.
Developer
precisionpete
Posts: 264
Registered: ‎10-05-2010
My Device: Not Specified

Re: How to get a TimerTask to schedule itself?

Can't do fixed rate as it is a variable rate. 

 

I suppose the problem is you can't schedule it from within itself?

Please use plain text.
Developer
Developer
CMY
Posts: 1,123
Registered: ‎02-10-2009
My Device: 8130 / 8350 / 9530 / 9550 / 9850 / PlayBook

Re: How to get a TimerTask to schedule itself?

I update my post with an example of how you could do it.

Please use plain text.
Developer
ahsanshaikh
Posts: 63
Registered: ‎10-25-2010
My Device: 9630

Re: How to get a TimerTask to schedule itself?

 

import java.util.Timer;
import java.util.TimerTask;




class TimerHelp extends TimerTask
{           
             
               public TimerHelp() 
               {                        
                       Timer timer = new Timer();
                       timer.schedule(this,2000,  1000);
               }

                public void run() 
                {                       
                        
                }

    }

 

 

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: How to get a TimerTask to schedule itself?

 


ahsanshaikh wrote:

 

import java.util.Timer;
import java.util.TimerTask;




class TimerHelp extends TimerTask
{           
             
               public TimerHelp() 
               {                        
                       Timer timer = new Timer();
                       timer.schedule(this,2000,  1000);
               }

                public void run() 
                {                       
                        
                }

    }

 

 


 

Anything like this will schedule a task at regular intervals, which the original poster specifically tried to avoid...

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.
Developer
precisionpete
Posts: 264
Registered: ‎10-05-2010
My Device: Not Specified

Re: How to get a TimerTask to schedule itself?

CMY: That model worked great... Thanks

Please use plain text.