Login  Register

Re: IJ.wait() and threads

Posted by Michael Schmid on Jun 04, 2009; 8:56am
URL: http://imagej.273.s1.nabble.com/IJ-wait-and-threads-tp3692285p3692286.html

Hi Tony,

besides using wait() and notify() as mentioned by Johannes, you could  
also have a look at ij.gui.WaitForUserDialog, maybe it is suited for  
your needs.

Michael
________________________________________________________________

On 4 Jun 2009, at 08:00, Tony Shepherd wrote:

> 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();
>
>    }
>  }
>
>
> }
> }