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 Contributor
tferguson
Posts: 7
Registered: ‎08-11-2010
My Device: Curve 8520 and Bold 9700
My Carrier: Rogers

Proper Looping in Apps

Good day all,

 

I am currently in the process of writing an application to monitor the output of certain services on our company server.

 

I'm rather new to writing apps for blackberrys so I need alittle help with a problem.

 

My code works, but when I run the app on the phone it locks out all user input. I believe this is because my code is never returning to the main method, because my code loops infinately inside a class function.

 

Example Pseudo-code:

 

while(true)

{

   /* do check of Server Services via HTTP Connection*/

  sleep(30000);

}

 

What is the proper method of running my check every 30 seconds without locking out the device? I only need help with the looping, the rest of my code works.

 

Any help would be greatly appeciated.

Please use plain text.
Developer
BB-Dude
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: Proper Looping in Apps

Run your code on a separate thread not the UIThread.

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

Re: Proper Looping in Apps

... or create a Timer, wrap your code in a TimerTask and schedule it to run every 30000 ms.  Effectively you are also using another Thread, but you don't call those sleep()s yourself...

----------------------------------------------------------
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
simon_hain
Posts: 16,010
Registered: ‎07-29-2008
My Device: Z10 LE
My Carrier: O2 Germany

Re: Proper Looping in Apps

this is not correct @arkadyz, timer should not be used to execute blocking operations.

see http://www.blackberry.com/developers/docs/5.0.0api/java/util/Timer.html

 

a separate thread is the correct approach, you can use a while(true) look or use a boolean to be able to cancel it.

 

notice that a poll every 30s will kill the battery very fast.

i would strongly suggest to use a push technique. on bes devices BES-push (very easy), on others BIS-push (quite new) or a trigger email. your device would then poll the server for the announced update, no need to send big data in the trigger message.

leverage the strengths of blackberry, push is one of them.

 

 

----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Please use plain text.
New Contributor
tferguson
Posts: 7
Registered: ‎08-11-2010
My Device: Curve 8520 and Bold 9700
My Carrier: Rogers

Re: Proper Looping in Apps

Thanks, I'm really surprised at how fast I got replies.

 

I'll look into running on a different thread. Can you provide a good example of running code on a different thread or point me in the direction of a good tutorial?

 

 

 

 

Please use plain text.
New Contributor
tferguson
Posts: 7
Registered: ‎08-11-2010
My Device: Curve 8520 and Bold 9700
My Carrier: Rogers

Re: Proper Looping in Apps

I found a good example. My Code is now working properly, and checking at the correct intervals.

 

Thanks for the help.

 

:smileyhappy:

 

Cheers

Please use plain text.
Developer
BB-Dude
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: Proper Looping in Apps

BTW Welcome to the BlackBerry Forums :smileyvery-happy:

Please use plain text.
New Contributor
tferguson
Posts: 7
Registered: ‎08-11-2010
My Device: Curve 8520 and Bold 9700
My Carrier: Rogers

Re: Proper Looping in Apps

Thanks :smileyhappy:

Please use plain text.