EDT violation: loading/editing image

32 views
Skip to first unread message

Matthias Bay

unread,
Feb 23, 2015, 4:44:44 PM2/23/15
to codenameone...@googlegroups.com
Hi,


I have a custom Form and I want to load an Image, edit it and show it. It looks like this:
public class MyForm extends Form{

   
private Image img;
   
private boolean ready;

   
public MyForm(){
        ready
= false;
       
new Thread(new Runnable() {
           
// load img and edit it
            ready
= true;
       
}).start();
   
}

   
public void paint(Graphics g){
       
if (ready){
           
//draw img
       
}else{
           
//draw "loading"
       
}
   
}
}

This actually works fine and looks good for me, but I get an EDT-violation.
I also get the EDT-violation when using Display.getInstance().startThread(...).start();
When using Display.getInstance().callSerially(...); or Display.getInstance().callSeriallyAndWait(...); I don't get the EDT-violation.
When editing the Image, I call img.getRGB(), which seems to lead to the violation.

Is it a false alarm? (getRGB leads to EDT-violation when not running on the EDT ?)
Which method should I use for loading and editing the Image?
Is there a difference between new Thread(r).start(); and Display.getInstance().startThread(r, "name").start(); ?


Best regards, Matthias

Shai Almog

unread,
Feb 24, 2015, 1:29:51 AM2/24/15
to codenameone...@googlegroups.com
Hi,
its hard to detect EDT violations. Image loading should generally work on a separate thread.
I'd rather have an EDT warning that is a false alarm than not have one when the real thing is happening.

Specifically what you are doing should be fine but as is true for any threading code I suggest extensive device testing.
Reply all
Reply to author
Forward
0 new messages