Login  Register

IJ.wait() and threads

Posted by Tony Shepherd on Jun 04, 2009; 6:00am
URL: http://imagej.273.s1.nabble.com/IJ-wait-and-threads-tp3692285.html

Dear users,
I am developing a plugin which needs to use the 'wait' command, to respond
to users interacting with the image window (using roi tools).

The wait-inside-while-loop method below causes the thing to hang up if it
is placed inside the main run() class.
However if I put it behind a separate runnable button then it works.
The problem is then I would start new threads every time the button is
pressed.

Can anyone shed light on this?

Thanks:.....

public class pseudo_code implements PlugIn {

  public void run(String s) {

  // DOESNT WORK IF I PUT IT HERE------------


       while(!finished)
       {
         IJ.wait(100);
         if(interaction) do something;
       }


  // WORKS INSIDE THE BUTTON BELOW------------

  myButton.addActionListener(new ActionListener() {
  public void actionPerformed(ActionEvent e) {

     Thread t1 = new Thread(new Runnable() {
     public void run() {

       while(!finished)
       {
         IJ.wait(100);
         if(interaction) do something;
       }

      }// thread run          
      });// new thread
      t1.start();

   }
 }


}
}