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


Thank you for visiting the BlackBerry Support Community Forums.

BlackBerry will be closing the BlackBerry Support Community Forums Device Forums on April 1st (Developers, see below)

BlackBerry remains committed to providing excellent customer support to our customers. We are delighted to direct you to the CrackBerry Forums, a well-established and thorough support channel, for continued BlackBerry support. Please visit http://forums.crackberry.com or http://crackberry.com/ask. You can also continue to visit BlackBerry Support or the BlackBerry Knowledge Base for official support options available for your BlackBerry Smartphone.

"When we launched CrackBerry.com 10 years ago, we set out to make it a fun and useful destination where BlackBerry Smartphone owners could share their excitement and learn to unleash the full potential of their BlackBerry. A decade later, the CrackBerry community is as active and passionate as ever and I know our knowledgeable members and volunteers will be excited to welcome and assist more BlackBerry owners with their questions."

- Kevin Michaluk, Founder, CrackBerry.com

Developers, for more information about the BlackBerry Developer Community please review Join the Conversation on the BlackBerry Developer Community Forums found on Inside BlackBerry.


Reply
New Contributor
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.

Developer
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: Proper Looping in Apps

Run your code on a separate thread not the UIThread.

Developer
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!
Developer
Posts: 17,025
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
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
New Contributor
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?

 

 

 

 

New Contributor
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.

 

Smiley Happy

 

Cheers

Developer
Posts: 516
Registered: ‎07-23-2010
My Device: 9900

Re: Proper Looping in Apps

BTW Welcome to the BlackBerry Forums Smiley Very Happy

New Contributor
Posts: 7
Registered: ‎08-11-2010
My Device: Curve 8520 and Bold 9700
My Carrier: Rogers

Re: Proper Looping in Apps

Thanks Smiley Happy