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
New Developer
Posts: 17
Registered: ‎02-06-2009
My Device: Not Specified
Accepted Solution

Problem in Timer and TimerTask for counter display at every 1 second on screen.

Dear Friends,

 

I have little problem in use of Timer. I have taken Timer to display counter on every fix period of time repeatedly. but I didn't succeed here. Please help me. The code is as shown below.

 

 

import java.util.Timer;

import java.util.TimerTask;

 

import net.rim.device.api.ui.Field;

import net.rim.device.api.ui.UiApplication;

import net.rim.device.api.ui.component.RichTextField;

import net.rim.device.api.ui.container.MainScreen;

 

public class TimerTest extends UiApplication 

{

  Timer timer;

  public static int COUNT =0; 

public static void main(String[] args) {

 

         TimerTest tt= new TimerTest();

         tt.enterEventDispatcher();

 

}

 

public TimerTest()

{

 //display a new screen

        pushScreen(new TimerMainScreen());

 

}

 

public class TimerMainScreen extends MainScreen

{

TimerMainScreen()

{  

add(new RichTextField("Count = " + Integer.toString(COUNT) ,Field.NON_FOCUSABLE));

try{

 

timer = new Timer();

 

timer.scheduleAtFixedRate(new TimerMainEvent(),200,1000);

 

}

catch(Exception e)

{

//do nothing

}

 

}

 

private class TimerMainEvent extends TimerTask

{

public void run()   

{

 

          try

          {

       

          COUNT++;

 

          if (COUNT <=44)

          add(new RichTextField("Count = " + Integer.toString(COUNT) ,Field.NON_FOCUSABLE));

          else

          timer.cancel();

              //timer.cancel();  

                    

          }

          catch(Exception e)

          {

        //Do nothing   

          }

            

         }  

}

}

 

 

}

 

 

Please let me know where I am wrong, I want counter display at every 1 second on screen. If I set Delay time 0 then it will execute TimerMainScreen  first time but after no any update I can see on the screen.

 

Thank in advance,

Vimal 

Developer
Posts: 3,951
Registered: ‎07-15-2008
My Device: Not Specified

Re: Problem in Timer and TimerTask for counter display at every 1 second on screen.

Use the following Code in your run.

UiApplication.getUiApplication().invokeLater(new Runnable() { public void run() { add(new RichTextField("Count = " + Integer.toString(COUNT) ,Field.NON_FOCUSABLE)); } });

 


Use Search. "Accept Solution" If the problem is resolved.
Developer
Posts: 238
Registered: ‎08-26-2008
My Device: Not Specified

Re: Problem in Timer and TimerTask for counter display at every 1 second on screen.

Hi,

 

at first try the schedule() method instead of the scheduleAtFixedRate() method.

Does it work now??

 

Why do you use/need scheduleAtFixedRate()?

 

Regards,

Jochen

 

 

New Developer
Posts: 17
Registered: ‎02-06-2009
My Device: Not Specified

Re: Problem in Timer and TimerTask for counter display at every 1 second on screen.

Thanks BBDeveloper,

 

It works for me. 

 

With Regards, 

Vimal 

New Developer
Posts: 17
Registered: ‎02-06-2009
My Device: Not Specified

Re: Problem in Timer and TimerTask for counter display at every 1 second on screen.

Thanks Jochen,

 

Yes now it works for me after BBDeveloper's solution code.

 

First I don't know what to use.

 

Regards,

Vimal 

New Developer
Posts: 2
Registered: ‎11-17-2008
My Device: Not Specified

Re: Problem in Timer and TimerTask for counter display at every 1 second on screen.

Thanks, it helped me as well.