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 › mousePressed() hangs the program - consistent
Page Index Toggle Pages: 1
mousePressed() hangs the program - consistent (Read 936 times)
mousePressed() hangs the program - consistent
May 18th, 2009, 8:34pm
 
Here is the trouble:
I have processing execute a lengthy code (analyzing a dozen of frames in a fraction of a second) at the click of the mouse inside mousePressed().

Here is the problem:

If I click the mouse briefly, the program hangs; if I take it easy with releasing the mouse button, no problem occurs. I suspect the mouseReleased() is to be blamed. I don't use mouseReleased(), just mousePressed() but I wonder how they intercept each other, like if the execution is within mousePressed() while the mouse is released, how the program hangs? I've performed more than 20 trials and the problem is consistant. I'm looking for understanding/solution to this. My target users are all teenagers and they may click their mice very fast. Thanks.
Re: mousePressed() hangs the program - consistent
Reply #1 - May 19th, 2009, 2:13am
 
Without seeing the code it's not easy to know what's causing the conflict; however I guess you could use the state of a variable to ensure that the mousePressed code has finished before mouseReleased does its thing:

Code:
int counter = 0;
int arraylength = 1000000;
float[] foo = new float[arraylength];

// extra variable to track mousePressed process
boolean processComplete = false;

void setup() {

}

void draw() {
   
}

void mousePressed() {
 // process isn't complete  
 processComplete = false;
 
 // make it do something that takes time
 // (I'm sure there must be a 'standard' way of doing this?)
 for(int i=0; i<arraylength; i++) {
   foo[i] = round(sin(sqrt(((i/10) * i+random(1,100)))));
   counter ++;
 }
 println(counter);
 // confirm it's complete
 processComplete = true;  
}

void mouseReleased() {
   while (processComplete == false) {
     // do nothing...
   }
   // now you can proceed...
   println("mouse released");
 
}


Obviously in this case mouseReleased() has no dependency on what's happening in mousePressed(), but I reckon this should work.
Re: mousePressed() hangs the program - consistent
Reply #2 - May 19th, 2009, 11:52am
 
When you use the mouse then you are generating events which are trapped by Processing and we can add our code in methods such as mousePressed() and mouseReleased() etc to perform some task.

Even if we don't include a particular method e.g. mouseReleased() the event is still being produced but it is handled by Processing.

When you press the mouse button and release it without moving the mouse position it generates 3 events

mousePressed
mouseReleased
mouseClicked

in that order (try the code below).  Note if the mouse has moved then you only get the first 2 events.

The important thing to note is that events are queued and processed one at a time, the code for each event being executed in full before the next event is taken off the queue and processed. So the mouseReleased code cannot be executed until the mousePressed code finishs. Which makes your problem interesting and I suspect the problem is caused by something else.

If you try the following code you will see what I mean

Code:

int arraylength = 5000000;
float[] foo = new float[arraylength];

void setup(){
 size(300,300);
}

void draw(){
}

void mousePressed(){
 println("mouse pressed");
 for(int i=0; i<arraylength; i++) {
   foo[i] = round(sin(sqrt(((i/10) * i+random(1,100)))));
 }
 println("mouse pressed end");
}

void mouseReleased(){
 println("mouse released");
}

void mouseClicked(){
 println("mouse clicked");
}


The "mouse pressed end" message always appears before the "mouse released" message.

There is an alternative way to handle mouse events in Processing and the code shows how to do it. You might try this approach and see if it fixes the problem.

Code:
int counter = 0;
int arraylength = 5000000;
float[] foo = new float[arraylength];


void setup(){
 size(300,300);
 registerMouseEvent(this);
}

void draw(){
}

public void mouseEvent(MouseEvent event){

 switch(event.getID()){
 case MouseEvent.MOUSE_PRESSED:
   println("mouse pressed");
   for(int i=0; i<arraylength; i++) {
     foo[i] = round(sin(sqrt(((i/10) * i+random(1,100)))));
   }
   println("mouse pressed end");
   break;
 case MouseEvent.MOUSE_CLICKED:
   println("mouse clicked");
   break;
 case MouseEvent.MOUSE_RELEASED:
   println("mouse released");
   break;
 }
}


Hope this helps.  Smiley
Page Index Toggle Pages: 1