Pablo
Pablo

Reputation: 3467

Network Thread blocking UI

I'm developing an application for Blackberry wich uses networking capabilities. The request is executed by pushing a button, with the following code:

mainButton=new BitmapButton(Bitmap.getBitmapResource("elcomercio.​gif"), Bitmap.getBitmapResource("elcomercio.gif"), Field.FOCUSABLE){
        protected boolean navigationClick(int status,int time) {
            //It automatically add itself to the screen stack
            waitScreen=new WaitScreen();
            TimeOutThread timeOut=new TimeOutThread(HomeScreen.this, 10000);
            HttpHelper helper=new HttpHelper("http://www.elcomercio.com/rss/latest", null, HomeScreen.this, HttpHelper.GET);
            UiApplication.getUiApplication().invokeLater(timeO​ut);
            UiApplication.getUiApplication().invokeLater(helpe​r);
            return true;
        }
    };

As you can see TimeOutThread and HttpHelper both inherit from Thread, so that they can be invoked outside the main flow of execution. Also both of them receive the current Screen as a delegate object, so that I can execute methods later on the screen. in this case timeout executes the following function.

public void onTimeout() {
    if(!didTimeout.booleanValue()){
        UiApplication.getUiApplication().popScreen(waitScr​een);
        didTimeout=Boolean.TRUE;
    }
}

The timeout method is called sucessfully... even the waitScreen is poppedOut sucessfully and the last screen is showed. But the UI hangs at that moment... is like the HttpThread I had is still executing blocking the UI... I know it because when the network thread times out... the UI is responsive again. What I'm doing wrong??.

Upvotes: 0

Views: 263

Answers (1)

Remy Lebeau
Remy Lebeau

Reputation: 595991

UiApplication.invokeLater() does not run a Thread object in its own execution thread. It runs the object in the main event dispatch thread instead - the same thread that runs your UI. You need to use the Thread.start() method instead of the UiApplication.invokeLater() method, eg:

mainButton = new BitmapButton(Bitmap.getBitmapResource("elcomercio.​gif"), Bitmap.getBitmapResource("elcomercio.gif"), Field.FOCUSABLE)
{
    protected boolean navigationClick(int status,int time)
    {
        waitScreen = new WaitScreen();
        TimeOutThread timeOut=new TimeOutThread(HomeScreen.this, 10000);
        HttpHelper helper = new HttpHelper("http://www.elcomercio.com/rss/latest", null, HomeScreen.this, HttpHelper.GET);
        timeO​ut.start();
        helpe​r.start();
        return true;
    }
};

Upvotes: 1

Related Questions