×

Discussion Board

Results 1 to 8 of 8
  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    35

    need help: Sleep in canvas class

    Hi guys,
    In my midlet application I have a canvas class which will act as a splash screen.
    I want to display the splash screen for 5 sec then send sms finally back to the main screen automatically.
    For the purpose I have used the code. Please have a look
    Code:
         public class SplashCanvas extends Canvas {
            // constractor
            public SplashCanvas ( MainClass mainClass) {
                          
            }
    
            public void delayMaker(){
                              
                //sleep for 5000 ms
                try{
                     System.out.println("in sleep");
                    Thread.currentThread().sleep(5000);
                     System.out.println(" after sleep");
                }catch( InterruptedException ie ){
                    System.out.println(ie.toString());
                }
                
                //send sms
                new Thread(new senderer()).start();
    	    
           
             // Here canvas is the main display canvas		     
    	 disp.setCurrent(canvas);
            }
            // Dispplay in the screen
            protected void paint(Graphics g) {
                Image image = null;
                // Fill the background using black
                g.setColor(0);
                // Load an image from the MIDlet resources to set as background
                if (image == null) {
                    try {
                        // Get the TC image
                        image=resizeImage(Image.createImage("/image/myImage.png"), width, height );
                    } catch (IOException ex) {
                        g.setColor(0xffffff);
                        g.drawString("Failed to load image!", 0, 0, Graphics.TOP | Graphics.LEFT);
                        return;
                    }
                }
                // Dispplay the image in the screen
                g.drawImage(image, 0, 0, Graphics.TOP | Graphics.LEFT);
                 delayMaker();
            }
        }

    But the problem is when i run the application the it will enter in the sleep before the display the
    splash screen then send sms.
    Some time it will not display the splash screen just wait and send sms.

    Please help me.
    thanks in advance.

    --- Eros.

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

    Re: need help: Sleep in canvas class

    This is because your sleep() is called (indirectly) from your paint() method. paint() is an event, and you should always return from event methods as quickly as you can. Never sleep() in an event! Where paint() is double-buffered (almost all devices), nothing will appear on the screen until paint() returns. By sleep()ing, you are preventing paint() from returning, and so preventing anything from appearing on the screen.

    Don't use createImage() in paint() either. This also delays returning from the event, and will cause variations in behaviour as you move the application to different devices. Don't do anything in paint(), except painting.

    Graham.

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    35

    Re: need help: Sleep in canvas class

    Quote Originally Posted by grahamhughes View Post
    This is because your sleep() is called (indirectly) from your paint() method. paint() is an event, and you should always return from event methods as quickly as you can. Never sleep() in an event! Where paint() is double-buffered (almost all devices), nothing will appear on the screen until paint() returns. By sleep()ing, you are preventing paint() from returning, and so preventing anything from appearing on the screen.

    Don't use createImage() in paint() either. This also delays returning from the event, and will cause variations in behaviour as you move the application to different devices. Don't do anything in paint(), except painting.

    Graham.
    thx Graham for your reply.
    So what should I do to delay the splash screen 5 sec? any idea.

    Regards,

    Eros

  4. #4
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: need help: Sleep in canvas class

    Put your delayMaker(); call outside the paint() just before you call for your main canvas
    delayMaker();
    // Here canvas is the main display canvas
    disp.setCurrent(canvas);

    thanks,
    ~Amitabh

  5. #5
    Registered User
    Join Date
    Oct 2008
    Posts
    35

    Re: need help: Sleep in canvas class

    Quote Originally Posted by im2amit View Post
    Put your delayMaker(); call outside the paint() just before you call for your main canvas
    Thx for ur reply. Here in the canvas class( splash screen ) I don't want to place the keyPressed event because it need key pressed.So how can I set the current display to the main canvas?
    I think commonAction will not also help any more in this situation.
    Any Idea?

    Thx again.


    Eros

  6. #6
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: need help: Sleep in canvas class

    public void delayMaker(){

    //sleep for 5000 ms
    try{
    System.out.println("in sleep");
    Thread.currentThread().sleep(5000);
    System.out.println(" after sleep");
    }catch( InterruptedException ie ){
    System.out.println(ie.toString());
    }

    //send sms
    new Thread(new senderer()).start();


    // Here canvas is the main display canvas
    disp.setCurrent(canvas);
    }
    Move this out of this splashCanvas and make a seperate thread of it, and start this thread from the paint of splashScreen, now it will not block splash painting() and will work.

    thanks,
    ~Amitabh

  7. #7
    Registered User
    Join Date
    Oct 2008
    Posts
    35

    Re: need help: Sleep in canvas class

    Thanks a lot Amitabh.Its work for me.
    Regards.

    Eros.

  8. #8
    Nokia Developer Champion
    Join Date
    Feb 2009
    Location
    Noida, India
    Posts
    3,073

    Re: need help: Sleep in canvas class

    Read this article, it will help you make your canvas app's better:

    http://wiki.forum.nokia.com/index.ph...d_applications

    thanks,
    ~Amitabh
    PS: Its my contribution to FN Wiki

Similar Threads

  1. Issue when trying to generate .sis file
    By rmulam in forum Symbian Tools & SDKs
    Replies: 9
    Last Post: 2009-08-13, 14:10
  2. Convert pure C++ class to Symbian C++ class
    By symbi4n_programmer in forum Symbian
    Replies: 4
    Last Post: 2009-02-28, 21:22
  3. Problem with setFullScreenMode(true) in canvas
    By mramin05 in forum Mobile Java Media (Graphics & Sounds)
    Replies: 6
    Last Post: 2008-12-06, 05:45
  4. does nokia handset support standard Canvas class
    By bright.chen in forum Mobile Java General
    Replies: 2
    Last Post: 2004-04-28, 09:18
  5. Major problem with delegation to Canvas
    By bartekn in forum Mobile Java Media (Graphics & Sounds)
    Replies: 8
    Last Post: 2003-10-08, 17:46

Posting Permissions

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