×

Discussion Board

Results 1 to 3 of 3
  1. #1
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Thread.sleep in while loop

    Could somebody explain me, why that short midlet doesn’t work as it should.
    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    public class Abcdef extends MIDlet implements CommandListener {
        private Command exitCommand;
        private Form dataViewForm;
        int x;
        public Abcdef() {
    		dataViewForm = new Form("Serial Data Viewer");
            exitCommand = new Command("Exit", Command.EXIT, 1);
            dataViewForm.addCommand(exitCommand);
            dataViewForm.setCommandListener(this);
            for(x=0;x<10;x++){
            	dataViewForm.append("ABCDEFGH \n");
            	System.out.println("ABCDEFGH");
            	try{
               		Thread.sleep(1000);
            	}catch(InterruptedException e){}  
        	}
        }
        protected void startApp() {
            Display.getDisplay(this).setCurrent(dataViewForm);
        }
    
        protected void pauseApp() {}
        protected void destroyApp(boolean bool) {}
    
        public void commandAction(Command cmd, Displayable disp) {
            if (cmd == exitCommand) {
                destroyApp(false);
                notifyDestroyed();
            }
        }
    }
    The result should looks like this:
    Code:
    ABCDEFGH
    // 1 sec. Pause
    ABCDEFGH
    // 1 sec. Pause
    ABCDEFGH
    // 1 sec. Pause
    ABCDEFGH
    // 1 sec. Pause
    ABCDEFGH
    // 1 sec. Pause
    ABCDEFGH
    // 1 sec. Pause
    ABCDEFGH
    // 1 sec. Pause
    ABCDEFGH
    // 1 sec. Pause
    ABCDEFGH
    // 1 sec. Pause
    ABCDEFGH
    But the result is:
    Code:
    //10 sec. Pause
    ABCDEFGH
    ABCDEFGH
    ABCDEFGH
    ABCDEFGH
    ABCDEFGH
    ABCDEFGH
    ABCDEFGH
    ABCDEFGH
    ABCDEFGH
    ABCDEFGH
    On the console WTK result is ok, but on emulator WTK result isn’t ok.

  2. #2
    Super Contributor
    Join Date
    Jun 2003
    Location
    Cheshire, UK
    Posts
    7,395

    Re: Thread.sleep in while loop

    Yup. Your expectation of what it should do is not correct.

    You have the loop in the constructor. Nothing will appear, because you haven't displayed the form yet. By the time startApp() is called, you have already added all the strings to the form, so it appears in its final state.

    Avoid putting any code in the MIDlet constructor.

    Also, avoid putting time-consuming code in startApp(). startApp() is an event, and is delivered through the event queue. The event queue delivers only one event at a time, so until an event method (like startApp()) returns, no other event will be delivered. The display is updated by events, so if you block startApp() from returning, nothing will be displayed.

    Here's a modified version:

    Code:
    import javax.microedition.midlet.*;
    import javax.microedition.lcdui.*;
    
    public class Abcdef extends MIDlet implements CommandListener, Runnable {
        private Command exitCommand;
        private Form dataViewForm;
    
        protected void startApp() {
            // this is important to stop the code from executing more than once
            if (dataViewForm == null) {
                // this is moved from the constructor
                dataViewForm = new Form("Serial Data Viewer");
                exitCommand = new Command("Exit", Command.EXIT, 1);
                dataViewForm.addCommand(exitCommand);
                dataViewForm.setCommandListener(this);
    
                // start a thread to perform the loop
                (new Thread(this)).start();
            }
    
            Display.getDisplay(this).setCurrent(dataViewForm);
        }
    
        protected void pauseApp() {
            // do nothing
        }
    
        protected void destroyApp(boolean mustExit) {
            // do nothing
        }
    
        public void commandAction(Command cmd, Displayable disp) {
            if (cmd == exitCommand) {
                destroyApp(false);
                notifyDestroyed();
            }
        }
    
        public void run() {
            for (int x = 0; x < 10; x++) {
                dataViewForm.append("ABCDEFGH \n");
                System.out.println("ABCDEFGH");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    // ignore
                }  
        	}
        }
    }
    Graham.

  3. #3
    Registered User
    Join Date
    Feb 2010
    Posts
    2

    Re: Thread.sleep in while loop

    very very thank You

Similar Threads

  1. Ask user permission while doing some process in loop
    By sandeepkumar03 in forum Mobile Java General
    Replies: 3
    Last Post: 2010-02-19, 13:50
  2. Thread.sleep() and Images
    By earamsey in forum Mobile Java General
    Replies: 3
    Last Post: 2007-08-30, 20:19
  3. RunL() method is not being called
    By dotcdotc in forum Symbian
    Replies: 9
    Last Post: 2004-12-13, 11:25
  4. Nokia 22- Loop current Reversal
    By Nokia_Archived in forum Nokia M2M
    Replies: 1
    Last Post: 2002-05-20, 14:39

Posting Permissions

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