display.setcurrent problem ?

Hi,

I have a strange problem with display.setcurrent syntax --

public void pleaseWaitForm() {

System.out.println("Inside pleaseWaitForm function");

pleaseWaitForm = new Form("Please wait request...");

pleaseWaitForm.setCommandListener(this);

display.setCurrent(pleaseWaitForm);

}

This is how i use the above method --

public boolean sendDataByPOST(String url, String x) throws IOException {

System.out.println("url: " + url);

System.out.println("data: " + x);

try {

threadData = x;

threadURL = url;

httpThread = new Thread(this);

httpThread.start();

pleaseWaitForm();

httpThread.join();

} catch (InterruptedException e) {

;

}

return true;

}

The http response from my webservere takes about 20 seconds (hence i want to show the "please wait .." message on the display screen ..

The strange problem that i'm facing is, even though the "pleaseWaitForm" gets called (debug trace stmts shows that every syntax in the pleaseWaitForm is executed), the emulator display screen does NOT show the "please wait ..." message !!! ? The entire display is frozzen till it get some response back from the webserver and then directly goes on to display the subsequent screens (bypassing the pleaseWaitForm screen) ..

The MIDP specs states that the setcurrent.display syntax executes the api and returns immediatly without waiting for the display to change .. (this should explain the above problem that i'm facing ) ..

so .. how do we display a "please wait" screen while processing the user request in the background ?

thankyou..

S.

[1723 byte] By [s990115] at [2007-9-26 8:31:51]
# 1

[s990115],

Here's what I think is happening, see below:

part of your try{} catch{} writes:

try {

threadData = x;

threadURL = url;

httpThread = new Thread(this);

httpThread.start();

pleaseWaitForm();

You may want to move the pleaseWaitForm(); line above the httpThread.start(); line as by starting the thread, it may have blocked the pleaseWaitForm().

httpThread.join();

} catch (InterruptedException e) {

;

}

return true;

}

Try the suggested changes and post the results of your test.

HTH.

Allen Lai

Developer Technical Support

SUN Microsystems

http://www.sun.com/developers/support/

allenlai at 2007-7-1 19:12:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 2

Allen,

I tried it the way you mentioned, but it still didnot work.

Actually, this is the main reason why i used threads. The idea is while the parent thread takes care of the pleaseWaitForm (displaying the please wait message), the spawned thread will take care of the http connection. Atleast I expected the display to be set to the 'please wait' screen .. but in-vain.

This is what i found in the midp specs -- FYI ..

setCurrent() is called whenever appliation wants a different Displayable object to be shown as the current screen. The change will NOT occur instantaneously. It is NOT guaranteed to happen before any more events have been delivered, will it will occur in the near future, between event deliveries.

Thanks,

Sandeepak.

s990115 at 2007-7-1 19:12:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 3

Hi Sandeepak,

When working with the Motorola i85s I wanted to use threads in a similar fashion - accessing the RMS via a Thread in the background and showing a "hang on" screen while the Thread is active.

As I started the background Thread, the phone spend all efforts on performing the task of the Thread, leaving no processing power for the main (ie. event-handling) Thread thus giving the phone no ability to show the "hang on" screen.

But as soon as I inserted a "Thread.sleep(xyz)" statement at the beginning of the run method of the Thread, everything worked fine (since the main thread now gets enough time to perform its task) ...

Perhaps you should try this - and try playing with the milliseconds-value you pass to the "Thread.sleep" method ... though I guess it should not be larger than somewhere around 500 millis.

Hope this helps,

.bbr

mppben at 2007-7-1 19:12:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 4

Thankyou for your reply.

I tried giving some delay in the run() method, but still the same problem. Maybe the way i'm using threads ?

I have pasted the relevent portions of my code in here .. but honestly, i don't see any thing wrong in it --

********* please wait screen **********

public void pleaseWaitForm() {

System.out.println("Inside pleaseWaitForm function");

pleaseWaitForm = new Form("Please wait ...");

pleaseWaitForm.setCommandListener(this);

display.setCurrent(pleaseWaitForm);

}

****** this is where the thread is created ****

public boolean sendDataByPOST(String url, String x) throws IOException {

System.out.println("Inside sendDataByPOST function");

System.out.println("url: " + url);

System.out.println("data: " + x);

try {

threadData = x;

threadURL = url;

httpThread = new Thread(this);

pleaseWaitForm();

httpThread.start();

httpThread.join();

} catch (InterruptedException e) {

System.out.println("Exception in sendDataByPOST : " + e);

}

return true;

}

******* actual thread ******

public void run() {

try {

Thread.sleep(5000);

} catch(InterruptedException e) {;}

System.out.println("Inside run thread");

.

.

. make actual http blocking calls

.

.

.

}

Maybe its the way this emulator behaves (midp-fcs from Sun and iDen85 emulator from motorola)

Thanks a lot in advance

sandeepak

s990115 at 2007-7-1 19:12:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...
# 5

Hi again!

What could also cause your problem is the call "httpThread.join();".

Though I'm not very familiar with the "join" method, I assume that it blocks as long as the Thread is not dead. You could verify this by placing a "System.out.println" statement right after it and see, whether it gets executed before or after the waiter-thread terminates.

Since the "display.setCurrent()" method only has an effect on the display when the event thread does not have to process your custom code any longer (eg. when the startApp or commandAction methods return).

Try sync'*** with the thread differently - let it call a certain method when it is done with its work to notify you that it has finished. Then remove the call to the "join" method and see if that works.

Hope this helps,

.bbr

mppben at 2007-7-1 19:12:35 > top of Java-index,Java Mobility Forums,Java ME Technologies...