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
Regular Contributor
DingDang
Posts: 61
Registered: ‎03-28-2010
My Device: Bold 9700
My Carrier: Maxis Malaysia

Timertask problem

Hi,

 

I'm using the following timertask to update time on every seconds

 

		try {
			timer = new Timer();
			timertask = new TimerTask() {
				public void run() {
					synchronized(UiApplication.getEventLock()){
						timestr.setText(" "+sdf.format(new Date())+" ");
					}
				}
			};
			timer.schedule(timertask, 1000, 1000);
		} catch (Exception e) {}

 

Problem is, everytime the timestr.setText is called, the input is getting a bit delayed. Like if I want to scroll my menu, it will get stuck and OK again after setText successfull (the second has been changed).

 

How to make sure this wont happen?

 

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

Re: Timertask problem

It's pausing because the TimerTask takes control of the EventLock, so the Ui cannot be updated until it releases it. I would make the TimerTask also responsible for updating the screen so that you do not have to hold the EventLock so often.

Please use plain text.
Regular Contributor
DingDang
Posts: 61
Registered: ‎03-28-2010
My Device: Bold 9700
My Carrier: Maxis Malaysia

Re: Timertask problem

How can I achieve that?

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

Re: Timertask problem

 

try {
			timer = new Timer();
			timertask = new TimerTask() {
				public void run() {
					//synchronized(UiApplication.getEventLock()){
						timestr.setText(" "+sdf.format(new Date())+" ");
                                                invalidate();
					//}
				}
			};
			timer.schedule(timertask, 1000, 1000);
		} catch (Exception e) {}

 

and make sure this is the only place where invalidate is called. You will probably also want to shorten the timer interval so everything moves smoothly.

 

Please use plain text.
Regular Contributor
DingDang
Posts: 61
Registered: ‎03-28-2010
My Device: Bold 9700
My Carrier: Maxis Malaysia

Re: Timertask problem

Thank you. But it returned IllegalStateexception.

Please use plain text.
Developer
anilsisodiya
Posts: 193
Registered: ‎04-27-2009
My Device: Not Specified

Re: Timertask problem

use invokelater for updating text.

Please use plain text.
Developer
kanna
Posts: 70
Registered: ‎01-02-2009
My Device: Not Specified

Re: Timertask problem

Use invokeLater ot update the text. For eg.,

 

 

UiApplication.getUiApplication().invokeLater(new Runnable() {
public void run() {
// code for updating the text
}
});

 

 

---------------------
Best Regards,
Kannabiran.
---------------------
Don't forget to mark your post as solved if you got answer. And give kudos.
Please use plain text.
Regular Contributor
DingDang
Posts: 61
Registered: ‎03-28-2010
My Device: Bold 9700
My Carrier: Maxis Malaysia

Re: Timertask problem

Invalidate give me exception error, and invokelater doesnt do anything. it is still stucky(?). kinda weird. :/

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

Re: Timertask problem

Where are you calling this code from? Directly inside the app or from within a thread?

Please use plain text.