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.

Posts: 23
Registered: ‎12-02-2012
My Device: BlackBerry 8300
My Carrier: Personal
Accepted Solution

Need help with this example code for updating the UI

I'm triying to understand the differences between the three methods for managing the UI interactions.
I'm really confused with these three terms when triying to figure them out in a real case.
I basically know that I must use invokeLater, invokeAndWat or getEventLock() to avoid
this exception: java.lang.illegalStateException: UI engine access without holding the event lock

The below code shows the function of the invokeAndWait method, but if I replace it by
invokeLater or getEventLock() the program will work exactly the same way.
Could you please modify the code in order to show the differences between the three
methods for updating the UI?

public final class HelloWorldMainScreen extends MainScreen{

          private LabelField labelField;
          public HelloWorldMainScreen() {
          labelField = new LabelField("Hello World");
          MainScreenUpdaterThread thread = new MainScreenUpdaterThread(this);

public void appendLabelText(String text){


public class MainScreenUpdaterThread extends Thread {
           HelloWorldMainScreen mainScreen;

           public MainScreenUpdaterThread(HelloWorldMainScreen mainScreen){
                  this.mainScreen = mainScreen;

            public void run(){
                   for (int i = 0; i < 10; i++) {
                          }catch(InterruptedException ex){};
                          UiApplication.getUiApplication().invokeAndWait(new Runnable() {
                                  public void run() {


These three concepts are very confusing for many starting people so any explanatory source code
describing their functions will be strongly helpful for anybody, I think.

Thanks in advance!

Posts: 206
Registered: ‎02-22-2011
My Device: BB TORCH & 9800
My Carrier: idea

Re: Need help with this example code for updating the UI

For differences between invokeAndWait() and invokeLater()

Go through the below thread for a while

* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: Need help with this example code for updating the UI

[ Edited ]

In terms of the effect, there is no difference between the methods.  The difference is the way the result is achieved.  So we can't change the code to show you the difference. 


Since we can't demonstrate the difference, you will have to make do with an explanation.  To understand the explanation, you will need to understand the Event Thread, so if you have not already, please review this:



So the three options are differentiated by the order of processing:

a) invokeLater runs the update on the Event Thread.  The processing takes place at some later stage and the code that is sequentially after the invokeLater will actually be executed before the code within the invokeLater. 

b) invokeAndWait also runs the update on the Event Thread, which means any other events that are waiting on the Event Thread will run before this code.  But any code after the invokeAndWait will not be be executed.

c) the synchronized option, like invokeAndWait, runs the UI update before moving on to the following code.  The difference is that code waiting to run on the Event Thread is not run before the code in the synchronized block. 


If this does not make sense, then than probably doesn't matter too much.  in general, you should use invokeLater, unless you need the UI update to occur in sequence with your background processing.  If so use invokeAndWait. There are very few occasions where you should use the synchronized block, and then it should be very small UI updates and you should understand the implications this might have on the Event Thread.