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
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified

How to create a periodic refresh thread?

I want to call a method every 30 seconds and repaint some screen stuff.

 

What is a clean way to wait X amount of time and then call a method?

 

I do not want to block anything on this screen, and I also would like it to stop when I exit the screen.

 

 

Eclispse 3.4
JDE 5
Bold/Tour/Storm
Please use plain text.
Regular Contributor
Abhijit_RIM
Posts: 98
Registered: ‎08-31-2010
My Device: BB-9700

Re: How to create a periodic refresh thread?

You can use TimerTask to achieve this kind of implementation.

 

Thanks

 

Regards

-Abhijit

Please use plain text.
Developer
Ted_Hopp
Posts: 1,305
Registered: ‎01-21-2009
My Device: Not Specified

Re: How to create a periodic refresh thread?

Like Abhijit said, a TimerTask (together with a Timer) is the tool you need. If all you are doing is calling invalidate(), you can do that directly in the run() method of the TimerTask. If you do anything more elaborate to the screen (such as update the text in a field), you need to either synchronize on the event lock within the run() method or else all invokeLater() on the app, passing a Runnable that will do the updating.

 

To close down the Timer when the screen exits, override the close() method:

 

protected void close() {
    // if the timer is used for other things, cancel just the task instead
    timer.cancel();
    super.close();
}

Don't override the onClose() method for this (a common mistake), for two reasons: (1) the screen can exit without that method being called and (2) the screen may not close even when the method is called.




Solved? click "Accept as solution". Helpful? give kudos by clicking on the star.
Please use plain text.
Trusted Contributor
ABoul
Posts: 138
Registered: ‎07-11-2010
My Device: Bold 9700

Re: How to create a periodic refresh thread?

you are in luck. i just coded one literally 20 seconds ago. insert this in your code:

 

 

private class Updater extends Thread
	{
		public void run()
		{
			while (true)
			{
				try {
					sleep(30000); // 30 seconds
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				synchronized(Application.getEventLock())
				{
					//here goes your update code
				}
			}
		}
	}

 

 

in your main code, just add this:

 

new Updater().start();

 

 

Please use plain text.
Developer
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified

Re: How to create a periodic refresh thread?

[ Edited ]

Wow that actually works great!

 

I am calling a method that creates a Runnable class and passes to a 'plesae wait' popup.

 

Very nice.

 

Anyone care to contrast the approaches?

 

Also, any disposal or thread issues with this latter approach?

 

Thanks.

 

 

>>>  FOLLOW UP

 

I do notice this continues to run after I exit the screen.

Eclispse 3.4
JDE 5
Bold/Tour/Storm
Please use plain text.
Trusted Contributor
ABoul
Posts: 138
Registered: ‎07-11-2010
My Device: Bold 9700

Re: How to create a periodic refresh thread?

[ Edited ]

 


shamusschubert wrote:

Wow that actually works great!

 

I am calling a method that creates a Runnable class and passes to a 'plesae wait' popup.

 

Very nice.

 

Anyone care to contrast the approaches?

 

Also, any disposal or thread issues with this latter approach?

 

Thanks.

 

 

>>>  FOLLOW UP

 

I do notice this continues to run after I exit the screen.


 

if you are referring to my code, you can make it stop by doing this:

 

create a boolean variable (something like "boolean shouldRun = true")

override the close event of your screen and set "shouldRun = false" there

in the Updater class, change the "while (true)" to "while (shouldRun)"

Please use plain text.
Developer
shamusschubert
Posts: 117
Registered: ‎10-22-2008
My Device: Not Specified

Re: How to create a periodic refresh thread?

Would this accomplish killing the wait as well?

 

	private class Updater extends Thread
	{
		public void run()
		{
			while (isDisplayed())
			{
				try {
					sleep(5000); // 30 seconds
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				synchronized(Application.getEventLock())
				{
					loadGames();
				}
			}
		}
	}

 

Eclispse 3.4
JDE 5
Bold/Tour/Storm
Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various

Re: How to create a periodic refresh thread?

[ Edited ]

Well, you have already found one difference - TimerTask has cancel() which a Thread lacks.

You can compensate for that by adding a public cancel() method to your custom Thread. The method will change some boolean flag (say, you have a boolean stillRunning member which you set to true as the first action of run() method and use while(stillRunning) instead of while(true); cancel() will have one statement - stillRunning = false;).

 

However, canceling either task or thread only within close() has a disadvantage of draining the battery faster.  Even if the screen goes black, the phone will not go into standby mode due to some task awakening and doing something every 30 sec.  Consider canceling the task/thread in onObscured() and starting a new one in onExposed().

 

Edit:

Finished my reply and saw a couple of new responses - I'll address that:

isDisplayed() will not work inside your thread since it is not the object being displayed. Besides, isDisplayed() returns true if the screen is on the display stack, no matter how many other screens may be on top of 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.
Trusted Contributor
ABoul
Posts: 138
Registered: ‎07-11-2010
My Device: Bold 9700

Re: How to create a periodic refresh thread?

[ Edited ]

 


shamusschubert wrote:

Would this accomplish killing the wait as well?

 

	private class Updater extends Thread
	{
		public void run()
		{
			while (isDisplayed())
			{
				try {
					sleep(5000); // 30 seconds
				} catch (InterruptedException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
				synchronized(Application.getEventLock())
				{
					loadGames();
				}
			}
		}
	}

 


you will run into problems when your application is minimised if you use that code.

 

Please use plain text.
Developer
dnepr
Posts: 723
Registered: ‎03-12-2009
My Device: Playbook

Re: How to create a periodic refresh thread?

I would use a timer task instead of a Thread.   Threads are a limited resources on BBs.

Please use plain text.