How to modify this example code in order to show the differences between the three methods for updating the UI in BlackBerry
Asked Answered
I

2

7

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.

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 someone 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!

Inulin answered 24/12, 2012 at 18:21 Comment(0)
V
6

My understanding for three different approaches:

  • Application.getEventLock() - get event lock as soon as possible
  • UiApplication.invokeLater() - put Runnable into event queue and it will be executed after all other tasks that were put before to this queue
  • UiApplication.invokeAndWait() - same as previous one except calling thread will be stopped until Runnable will be run

My personal opinion never use first method. I don't think that any of my draw or UI changes tasks are more prioritized that other tasks already put to event queue.

I use mostly second method and I used once last one when I implemented some modal popup dialog selection.

Vivien answered 24/12, 2012 at 20:30 Comment(0)
A
2

getEventLock() :Retrieves the application user interface event lock. Worker threads should synchronize on this thread if they wish to execute code serialized with the event thread. Your worker thread should hold the lock only for a short period of time, as this action pauses the thread dispatcher.

Any operation involving the device's user interface must be done with the lock held. The UI system also guarantees that any methods it invokes will execute on a thread that already has the lock.

An application should never call notify or wait on this object.

invokeLater(): Puts runnable object into this application's event queue. Invoke this method, passing a runnable object, to have that object's run() method invoked on the dispatch thread, after all pending events are processed.

If there is no event dispatch thread (ie. hasEventThread() returns false), then the last item to be queued is dropped when the queue surpasses its size limit. Note: If an application does not have an event thread, you may invoke setAcceptEvents(boolean) to inform the runtime system that the application no longer accepts events. All events queued to that application are then discarded.

invokeAndWait(): Puts runnable object into this application's event queue, and waits until it is processed. Invoke this method, passing a runnable object, to have that object's run() method invoked on the dispatch thread, after all pending events are processed.

This method blocks until the insert event is processed (that is, until the runnable object's run() method returns).

It is safe to call this method on the event dispatch thread. In this case the runnable will be executed immediately.

If there is no event dispatch thread (ie. hasEventThread() returns false), then the last item to be queued is dropped when the queue surpasses its size limit. Note: If an application does not have an event thread, you may invoke setAcceptEvents(boolean) to inform the runtime system that the application no longer accepts events. All events queued to that application are then discarded.

API documentation: http://www.blackberry.com/developers/docs/4.3.0api/net/rim/device/api/system/Application.html#invokeLater(java.lang.Runnable)

Amenable answered 26/12, 2012 at 17:49 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.