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
Contributor
mrdialect2012
Posts: 27
Registered: ‎05-10-2012
My Device: bold 9700
Accepted Solution

How do i Restart a TimerTask, I want to reuse an instance of TimerTask on a button click

but I understand once you call it once thats it. That cannot be reused.

 

I am getting invalid state errors everything I try and reuse the same instance of TimerTask, surely there must be a way of restarting it?

Developer
smiley
Posts: 1,497
Registered: ‎07-14-2008
My Device: Z10

Re: How do i Restart a TimerTask, I want to reuse an instance of TimerTask on a button click

You can just create the new timer again and if you need to repeat it every minute then just set the delay seconds when you create the timer.

Contributor
mrdialect2012
Posts: 27
Registered: ‎05-10-2012
My Device: bold 9700

Re: How do i Restart a TimerTask, I want to reuse an instance of TimerTask on a button click

I do create a new instance of timer, but timertask u cannot create a new instance.?, u can only make 1 instance

Developer
smiley
Posts: 1,497
Registered: ‎07-14-2008
My Device: Z10

Re: How do i Restart a TimerTask, I want to reuse an instance of TimerTask on a button click

do something like this:

 

public  Timer _timer = null;

elsewhere in your code

 

// you can call this repeatedly to create a new timer but

// if you want the timer to do something every x seconds then

// just  use this function scheduleAtFixedRate(TimerTask task, long delay, long period)

 

void start_timer()

{

                TimerTask myTask = new _myTimer();                
        
                _timer = new Timer();

                // delay and start after 1 second
                _timer.schedule(1000);       

}

 

public  class _myTimer extends TimerTask {
                
            public void run(){

                // do something
                }
 }


 

Developer
vjkmr
Posts: 78
Registered: ‎05-17-2012
My Device: 9900 - OS 7.0 Torch device

Re: How do i Restart a TimerTask, I want to reuse an instance of TimerTask on a button click

Hi,

Before you execute the code in run method of Timertask, cancel the timer and then let go to execute. whenever you need again, just start again with new object. If you not cancel, this timer may kill with its default time.
Contributor
mrdialect2012
Posts: 27
Registered: ‎05-10-2012
My Device: bold 9700

Re: How do i Restart a TimerTask, I want to reuse an instance of TimerTask on a button click

[ Edited ]

 DO you mean???????????????

 

public  Timer _timer = null;

   

void start_timer()

{

 

  _timer = new Timer();

_timer.schedule(x,1000); 


}

 

TimerTask x = new TimerTask()

{

_Timer.cancel();  <-------------------------

 public void run(){

                // do something
                }

 

};


Contributor
mrdialect2012
Posts: 27
Registered: ‎05-10-2012
My Device: bold 9700

Re: How do i Restart a TimerTask, I want to reuse an instance of TimerTask on a button click

and this can be reused as many times as I want, the timertask object right. So if I click a button and it gets used and then I click the button again say after 10 seconds it still does the same thing?

Developer
vjkmr
Posts: 78
Registered: ‎05-17-2012
My Device: 9900 - OS 7.0 Torch device

Re: How do i Restart a TimerTask, I want to reuse an instance of TimerTask on a button click

Hi

public class TimersThread
{
private Timer timer;
public TimersThread()
{
timer = new Timer();
timer.schedule(new TimerTask(){
public void run()
{
synchronized(UiApplication.getEventLock())
{
timer.cancel();

}
}
}, 1000);
}
Contributor
mrdialect2012
Posts: 27
Registered: ‎05-10-2012
My Device: bold 9700

Re: How do i Restart a TimerTask, I want to reuse an instance of TimerTask on a button click

The event lock and cancel of the timer happens after I execute my bit of code in the run right?

Developer
vjkmr
Posts: 78
Registered: ‎05-17-2012
My Device: 9900 - OS 7.0 Torch device

Re: How do i Restart a TimerTask, I want to reuse an instance of TimerTask on a button click

Once after you enter into the synchronized block, you need not to think about the timer again. because for your next use, you are going to call with new object. Hence this can be cancelled first, and then your bit of code will get executed. Have you tried it not ? I think, instead of having multiple queries, if you tried your code by debugging, this comes to know.