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
Contributor
nordavinder
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");
          add(labelField);
          MainScreenUpdaterThread thread = new MainScreenUpdaterThread(this);
          thread.start();
}

public void appendLabelText(String text){
          labelField.setText(labelField.getText()+"\n"+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++) {
                          try{
                                 Thread.sleep(5000);
                          }catch(InterruptedException ex){};
                          UiApplication.getUiApplication().invokeAndWait(new Runnable() {
                                  public void run() {
                                          mainScreen.appendLabelText("Update");
                                  }
                           });

                    }
              }
}


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!

Please use plain text.
Developer
tklanilkumar
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

http://supportforums.blackberry.com/t5/Java-Development/Update-a-screen-on-the-Main-Event-Thread/ta-...
*****************************************************************************
* Give kudos when you like it
* Mark it as a solution if it fixes your issue
Please use plain text.
Developer
peter_strange
Posts: 19,598
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:

http://supportforums.blackberry.com/t5/Java-Development/What-is-the-Event-Thread/ta-p/446865

 

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.

Please use plain text.