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
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified
Accepted Solution

Update screen, but still wait for thread to return

I have an app that makes updates to the UI by passing in a reference for a bitmapfield to a new thread, and that thread updates the UI.

 

The problem I am having is that I want the main thread "wait" for the sub-thread to update the graphics, before proceeding with the "game".

 

 so.... for example..

 

 

// update the main bitmapfield

pt = new PlayerThread(bfField,square); pt.start();

 

// wait for the thread to finish updating the ui

while (pt.isAlive()){}

 

 Now, I *KNOW* this is not the proper way to do this, because the UI does not get updated, since the UI is in a loop waiting for the thread to finish, which defeats the purpose of having the thread in the background in the first place.

 

What is the "correct" wait to do this? Have I completely missed the point of threading?

 

This works fine if I don't "wait" for the thread,but it means the user can press buttons, or interact with the game when they aren't supposed to.

 

I was thinking there must be a way to set a "isthreading = true" var, and disable button presses by adding an if to my keydown, but how can i make my app "wait" for the thread i created to be finished, but not actually pause the UI itself?

 

Hopefully that was clear...

 

 

Developer
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified

Re: Update screen, but still wait for thread to return

[ Edited ]

I just noticed Thread.yield(); ...

 

is that the proper way? ...  edit, this has the same effect as the while loop..

Message Edited by footose on 07-05-2009 12:36 PM
Message Edited by footose on 07-05-2009 12:39 PM
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Update screen, but still wait for thread to return

You need a notification mechanism that tells the UI thread "I'm done" or "data is ready". Never loop in a UI thread - this will eventually cause the event queue to overflow, resulting in you application being dumped out as "unresponsive".

 

A typical mechanism is to implement a listener and a data event. The "interested" party registers a listener with the background thread, the background thread fires the data event when appropriate.

 

If there is only one listener, then this can be implemented with less overhead by using a callback mechanism.

 

Look for references to the Observer pattern:

 

http://en.wikipedia.org/wiki/Observer_pattern

 

Developer
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified

Re: Update screen, but still wait for thread to return

Cheers
Developer
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified

Re: Update screen, but still wait for thread to return

I guess I would be looking for "responseListener" though?
Developer
Posts: 4,764
Registered: ‎07-21-2008
My Device: Not Specified

Re: Update screen, but still wait for thread to return

You have to "invent" it on the RIM platform.

 

 

Developer
Posts: 300
Registered: ‎03-12-2009
My Device: Not Specified

Re: Update screen, but still wait for thread to return

Is there an example ?