12-24-2012 10:32 AM
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!
Solved! Go to Solution.
12-24-2012 10:56 AM
12-27-2012 06:35 PM - edited 12-27-2012 06:47 PM
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-Develo
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.