Login  Register

Re: ImageListener

Posted by Philippe GENDRE on Feb 28, 2014; 4:53pm
URL: http://imagej.273.s1.nabble.com/ImageListener-tp5006715p5006719.html

Dear Michael,

Thank you for your very didactic and efficient answer.

According to do exactly what I want : to stop the program at any time by
user interaction (when the image is updated or not) and to avoid image
processing on not updated image after each timeout, I adapted the code like
this :

img.addImageListener(this);
waitingThread = Thread.currentThread();

do {

                doUpdate=false;
                synchronized(this){
                    while (doUpdate==false){
                        try {
                            wait(1000);
                            if (iwantToStopmyProgram==true) break;
                        }
                        catch(InterruptedException e) {
                            continue;
                        }
                    }
                }

//image processing on updated image

}while (iwantToStopmyProgram==false)

public synchronized void imageUpdated(ImagePlus img) {
        if (img == this.img && waitingThread != null){
            waitingThread.interrupt();
            doUpdate=true;
        }
}

Best regards,

Philippe



2014-02-28 15:35 GMT+01:00 Michael Schmid <[hidden email]>:

> > I would like to exit from a synchronized  while (image not updated)
> > wait() (until the image is updated) loop when the image is not updated.
>
> Hi Philippe,
>
> it seems that you need some timeout, otherwise you can't know whether an
> update is still to come.  Maybe roughly like this:
>
> //class variables ('global')
>     ImagePlus theImageIamWaitingFor;
>     Thread waitingThread;
>
>     public void imageUpdated(ImagePlus imp) {
>         if (imp == theImageIamWaitingFor && waitingThread != null)
>             waitingThread.interrupt();
>     }
>
> //main processing code
>     ...
>     waitingThread = Thread.currentThread();
>     theImageIamWaitingFor = ...
>     while (true) {   //might be also while (!closed) etc.
>         //do some analysis on updated image
>         synchronized(this) {
>             //wait for image update, but no longer than 1000 millesec:
>             try {
>                 wait(1000);
>             } catch (InterruptedException e) {
>                 continue; //loop again after update
>             }
>             break; //timeout, leave the while loop
>         }
>     }
>
> Michael
> ________________________________________________________________
> On Feb 28, 2014, at 14:05, Philippe GENDRE wrote:
>
> > Dear List,
> >
> > I would like to exit from a synchronized  while (image not updated)
> wait()
> > (until the image is updated) loop when the image is not updated.
> >
> > Is it possible and how to ?
> >
> > Thanks.
> >
> > Philippe
> >
> > --
> > ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>
> --
> ImageJ mailing list: http://imagej.nih.gov/ij/list.html
>

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html