We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › Why does this crash
Page Index Toggle Pages: 1
Why does this crash? (Read 483 times)
Why does this crash?
Apr 6th, 2006, 12:08am
 
I want the program to stop and do nothing until a mouse click is received.  Nothing I do seems to work, and my sketches just blow up.  Any ideas?  Thanks guys!

After lots of drawing, this is called from draw():

Code:

void PickBest(){
choosing=true;
while(choosing==true){
if(mousePressed==true) { choosing=false; }
}
}
Re: Why does this crash?
Reply #1 - Apr 6th, 2006, 12:44am
 
Basically, mousePressed will never become true.  You should not use an infinite loop in your draw() method ever, but instead do something like:

Code:

boolean choosing = true;

void draw() {
if (!choosing) {
//do whatever you normally do
} else {
if (mousePressed) choosing = false;
}
}
Re: Why does this crash?
Reply #2 - Apr 6th, 2006, 1:19am
 
>You should not use an infinite loop in your draw() method

I was afraid of that.  Although I don't see why it should be the case.  Especially if having used noLoop().
Re: Why does this crash?
Reply #3 - Apr 6th, 2006, 1:36am
 
when the applet is run, and it gets to the while loop, it stops, because you can't get two types of userinteraction on one frame.. one frame, the mouse isn't clicked, the next it might be.. This is why your code never gets to the end of the draw() or setup() function, where the screen is drawn
Re: Why does this crash?
Reply #4 - Apr 6th, 2006, 1:36am
 
Well, the program just gets stuck in the loop, and it never gets out of the loop to check if the mouse was pressed.  It may be possible if you use void mousePressed() { choosing = false; }, but it may also have unexpected results (since processing isn't designed as threadsafe, as far as I know).

Marcello
Re: Why does this crash?
Reply #5 - Apr 6th, 2006, 3:52pm
 
the mousePressed var is thread safe, but that means it only gets updated outside of draw(). the mousePressed() method is also thread safe, but may or may not be called in that situation, it sort of depends on the machine: in most cases, the while() loop is so tight that it'll spin out of control and "starve" any other threads because it never takes a breath (which would let the ui thread update itself).

on more than one occasion, i've wanted to remove while() from the syntax for this reason. java's just not very good at dealing with these situations and it means a lot of force-quitting the vm for someone who's not familiar with the issue.
Re: Why does this crash?
Reply #6 - Apr 6th, 2006, 10:09pm
 
Thanks for the good info fry.  Thread starving is exactly what I was witnessing. And it happens QUICKLY!  Now I can see why you want to remove while().

In this particular application, I was not needing (in fact, didn't want) any drawing to take place while waiting for a mouse click.  So I thought I would be safe to just "pause" the draw routine.  My rutted brain wanted to use a while loop so bad.

I rewrote it to use a state variable instead, which works very nicely, but took a little change in my thinking.  A good thing!  

FYI, here's the sketch I was working on, a genetic image mutator:

http://spacefillingcurve.net/programming/processing/mothevolve/

I LOVE PROCESSING!
Page Index Toggle Pages: 1