×

Discussion Board

Results 1 to 5 of 5
  1. #1
    Registered User
    Join Date
    Jul 2014
    Posts
    4

    Question I can't show a 'Wait' screen when I need to wait for a thread to be finished

    I need to show some kind of animation(progress indicator) when a task takes some time to complete. This is the code for my wait screen:

    Code:
    import javax.microedition.lcdui.Form;
    import javax.microedition.lcdui.Gauge;
    import javax.microedition.lcdui.Item;
    import javax.microedition.lcdui.StringItem;
    
        public class scrWaitForm extends Form {
    
            public static scrWaitForm create() {
                return new scrWaitForm();
            }
            private final Gauge gagProgressBar;
            private final StringItem strMessage;
    
            protected scrWaitForm() {
                super("Procesando");
                this.gagProgressBar = new Gauge("", false, Gauge.INDEFINITE, Gauge.CONTINUOUS_RUNNING);
                this.gagProgressBar.setLayout(Item.LAYOUT_CENTER| Item.LAYOUT_VCENTER);
                this.append(gagProgressBar);
                this.strMessage=new StringItem("Loading...", null);
                this.append(strMessage);
            }
        }
    And this is how I show it :

    Code:
        public void showWaitForm() {
            scrWaitForm frmWaitForm = scrWaitForm.create();
            mDisplay.setCurrent(frmWaitForm);
        }
    As you can see, it is very simple. I only use a Gauge. The problem is that the wait screen won't be displayed if I need to wait for a thread to be finished so that I can use some operation results(from the thread) in other operations. However, if I only call the wait screen without waiting for the thread to complete it works as expected.

    This is what I initially did:

    Code:
    thrLoadCustomers load = new thrLoadCustomers(rmsCustomers, url);
        Thread t = new Thread(load);
        showWaitForm()
        t.start();          
        try {
            t.join();
        } catch (InterruptedException ex) {
    
        }
    But then searching a little bit I ended up using wait/notify instead of join as shown below:
    Code:
    WSResult result = new WSResult();
    
    //Start thread here
    new Thread(new LoadCustomersTask(result)).start();
    
    //This is old school thread sync. 
    synchronized(result){
        showWaitForm();
        while(!result.isCompleted()){
            result.wait();
        }
    }
    //Here the thread has returned, and we can diaplay the error message if any
        if(result.isSuccess()){
    
        } else {
            //Display result.getErrorMessage()
        }
    This is more or less how my runnable looks like :

    Code:
    class LoadCustomersTask implements Runnable {
            private final WSResult result;
            public LoadCustomersTask(WSResult res){
                result = res;
            }
    
            public void run(){
    
                //Do the WS call
    
                //If it went well
                result.setSuccess(true);
                result.setResult(jsonResultados);
    
                //Else
                result.setSuccess(false);
                result.setErrorMessage("Error Message");
    
    
                //In any case, mark as completed
                result.setcompleted(true);
    
                //And notify awaiting threads           
                synchronized(result){
                    result.notifyAll();
                }       
            }
        }
    And this is the wrapper class WSResult :

    Code:
        public class WSResult {
            private boolean success; //true if the WS call went ok, false otherwise
            private String errorMessage; //Error message to display if the WS call failed.
            private Object result; //Result, only if the WS  call succeeded.
            private boolean completed = false;
    
            //TODO getter and setters methods here
        }
    Am I doing something wrong?? How do you normally show an animation or another screen so that user sees that something is taking place and doesn't intend to invoke the same action over and over again.

    Thanks in advance.

  2. #2
    Nokia Developer Moderator
    Join Date
    Feb 2006
    Location
    Oslo, Norway
    Posts
    28,752

    Re: I can't show a 'Wait' screen when I need to wait for a thread to be finished

    It is the join. UI in Java ME (both LCDUI and LWUIT) is built around an event dispatcher. It dispatches events one by one. Showing new Displayable-s is just an event, it does not happen immediately, but gets into the event queue. So when your event handler does not return (because it waits for the join), the thing will not get displayed while waiting. Later when it gets displayed, it will immediately going to be "overdisplayed" by the next screen. The same applies to the wait loop, it does not differ from join from this point of view.
    Check recent discussion, http://developer.nokia.com/community...e-return-value, it discusses very similar things, also there are some suggestions, though unverified.

  3. #3
    Registered User
    Join Date
    Jul 2014
    Posts
    4

    Re: I can't show a 'Wait' screen when I need to wait for a thread to be finished

    So if I call the thread using lcdui.Display.callSerially(Runnable r) , I will be able to show a 'wait' screen??
    I think you're right because sometimes when I execute the thread , right before the thread is complete I get to see something like a flash (guess it is the wait screen).
    Please tell me based on you're experience, how would you have handled such a scenario? would you use callSerially too?

    I'll try what you suggested and see what happens

    Thanks.

  4. #4
    Registered User
    Join Date
    Jul 2014
    Posts
    4

    Re: I can't show a 'Wait' screen when I need to wait for a thread to be finished

    Thank you wizard_hu_ callSerially works like a charm. (Y)

  5. #5
    Nokia Developer Moderator
    Join Date
    Oct 2006
    Location
    Finland
    Posts
    4,563

    Re: I can't show a 'Wait' screen when I need to wait for a thread to be finished

    FYI, this thread has been featured and you can find the blog of the same here

Similar Threads

  1. Replies: 2
    Last Post: 2012-10-21, 13:46
  2. Wait Dialog doesn't show
    By KSD in forum Symbian
    Replies: 12
    Last Post: 2010-03-19, 08:46
  3. Replies: 8
    Last Post: 2007-09-10, 18:52
  4. how to show wait dialog asynch...???
    By ibraheemJazba in forum Symbian
    Replies: 12
    Last Post: 2007-03-27, 17:08
  5. Replies: 4
    Last Post: 2005-11-10, 06:13

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
×