• Post Reply Bookmark Topic Watch Topic
  • New Topic

Need help with "run"or threading  RSS feed

 
Bob Ford
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The following code snippet displays the correct image in the emulator, but is slightly corrupted on the hardware:

protected void compute() {
// computationally intensive: over one second to execute
}
public void paint(Graphics g) {
...
g.drawRGB(xfrmImage, 0, POINTS, rx + 1, ry + 1, POINTS, POINTS, false);
}
public void run() {
int interval = 1000 * 2;
int count = 0;

while (mTrucking) {
System.out.println("count = " + count);
compute();
repaint();
try { Thread.sleep(interval); }
catch (InterruptedException ie) {}
if (count++ == 1)
mTrucking = false;
}
}
A hint of the error is in the print "count =" appearing twice:
count = 0
count = 0
count = 1
count = 1


The display, on the hardware, is fundamentally correct, but with visible errors. This suggest I do not adequately appreciate how "run" is implemented or how the threading works. It is as if a second "run" has been started before the first has finished. Any suggestions would be appreciated! Thanks,
 
Tim Holloway
Bartender
Posts: 18531
61
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I haven't got my GUI APIs crossed, you probably should be invoking invalidate(), not repaint().

Invalidate() schedules repainting of selected (damaged/updated) parts of the display to run on the GUI thread, whereas repaint() forces immediate action.

I think.
 
Bob Ford
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your reply. My understanding is that invalidate() is not available in java me. Apparently, this has confused folks in the past, so I'm not alone; I just haven't found the solution yet. I believe it is connected with the run method containing the loop. Further testing shows that paint is not executed until the loop is completed, irrespective of repaint() or even serviceRepaints() in that loop.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!