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 |
> 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 |
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 |
Hi Philippe,
it seems to me that there must be an event that sets your variable 'iwantToStopmyProgram'. In the code that sets 'iwantToStopmyProgram', instead of setting the variable, you could also interrupt a thread, notify a waiting thread, etc. So I think you don't need a timeout. But it is still unclear to me what exactly you want to do. Is it previewing the result of processing? ImageJ has all the infrastructure for previewing if you have an ExtendedPlugInFilter with a GenericDialog; you need not care about anything except for checking from time to time whether the processing thread was interrupted (simply abort processing in that case). Michael ________________________________________________________________ On Feb 28, 2014, at 17:53, Philippe GENDRE wrote: > 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 -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Dear Michael,
it seems to me that there must be an event that sets your variable > 'iwantToStopmyProgram'. > In the code that sets 'iwantToStopmyProgram', instead of setting the > variable, you could also interrupt a thread, notify a waiting thread, etc. > > So I think you don't need a timeout. > You have completely right. I simplified the code as : ImagePlus.addImageListener(this); waitingThread = Thread.currentThread(); do { synchronized(this){ try { wait(); } catch(InterruptedException e){ } //image processing } public synchronized void imageUpdated(ImagePlus imp) { if (imp == img); notify(); } the waiting thread is interupted clicking on a button. But it is still unclear to me what exactly you want to do. > Is it previewing the result of processing? > > ImageJ has all the infrastructure for previewing if you have an > ExtendedPlugInFilter with a GenericDialog; you need not care about anything > except for checking from time to time whether the processing thread was > interrupted (simply abort processing in that case). > I do ("real time") image processing on images coming from a camera live streaming which is triggered. I don't understand " if you have an ExtendedPlugInFilter with a GenericDialog; you need not care about anything except..." If you can explain more "you need not care about anything" I'd appreciate. Anyway I am very grateful for these exchanges which allowed me to progress. Best regards, Philippe 2014-02-28 18:08 GMT+01:00 Michael Schmid <[hidden email]>: > Hi Philippe, > > it seems to me that there must be an event that sets your variable > 'iwantToStopmyProgram'. > In the code that sets 'iwantToStopmyProgram', instead of setting the > variable, you could also interrupt a thread, notify a waiting thread, etc. > > So I think you don't need a timeout. > > But it is still unclear to me what exactly you want to do. > > Is it previewing the result of processing? > ImageJ has all the infrastructure for previewing if you have an > ExtendedPlugInFilter with a GenericDialog; you need not care about anything > except for checking from time to time whether the processing thread was > interrupted (simply abort processing in that case). > > Michael > ________________________________________________________________ > On Feb 28, 2014, at 17:53, Philippe GENDRE wrote: > > > 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 > > -- > ImageJ mailing list: http://imagej.nih.gov/ij/list.html > -- ImageJ mailing list: http://imagej.nih.gov/ij/list.html |
Hi Philippe,
ok, I see. Your solution is fine for the case of processing images in a live stream from a camera; the 'Preview' infrastructure of ImageJ does not handle such a case. What you can easily do with an ExtendedPlugInFilter and a GenericDialog with addPreviewCheckbox is previewing the result of an operation to try different parameters, i.e. what you get with the 'Preview' checkbox in most dialogs of the various commands in the Process menu of ImageJ. But that's another story... Michael ________________________________________________________________ On Mar 3, 2014, at 11:25, Philippe GENDRE wrote: > Dear Michael, > > it seems to me that there must be an event that sets your variable >> 'iwantToStopmyProgram'. >> In the code that sets 'iwantToStopmyProgram', instead of setting the >> variable, you could also interrupt a thread, notify a waiting thread, etc. >> >> So I think you don't need a timeout. >> > > You have completely right. I simplified the code as : > > ImagePlus.addImageListener(this); > waitingThread = Thread.currentThread(); > do { > synchronized(this){ > try { > wait(); > } > catch(InterruptedException e){ > } > //image processing > } > public synchronized void imageUpdated(ImagePlus imp) { > if (imp == img); > notify(); > } > > the waiting thread is interupted clicking on a button. > > But it is still unclear to me what exactly you want to do. >> Is it previewing the result of processing? >> >> ImageJ has all the infrastructure for previewing if you have an >> ExtendedPlugInFilter with a GenericDialog; you need not care about anything >> except for checking from time to time whether the processing thread was >> interrupted (simply abort processing in that case). >> > > I do ("real time") image processing on images coming from a camera live > streaming which is triggered. I don't understand " if you have an > ExtendedPlugInFilter with a GenericDialog; you need not care about anything > except..." If you can explain more "you need not care about anything" I'd > appreciate. > > Anyway I am very grateful for these exchanges which allowed me to progress. > > Best regards, > > Philippe > > > > > 2014-02-28 18:08 GMT+01:00 Michael Schmid <[hidden email]>: > >> Hi Philippe, >> >> it seems to me that there must be an event that sets your variable >> 'iwantToStopmyProgram'. >> In the code that sets 'iwantToStopmyProgram', instead of setting the >> variable, you could also interrupt a thread, notify a waiting thread, etc. >> >> So I think you don't need a timeout. >> >> But it is still unclear to me what exactly you want to do. >> >> Is it previewing the result of processing? >> ImageJ has all the infrastructure for previewing if you have an >> ExtendedPlugInFilter with a GenericDialog; you need not care about anything >> except for checking from time to time whether the processing thread was >> interrupted (simply abort processing in that case). >> >> Michael >> ________________________________________________________________ >> On Feb 28, 2014, at 17:53, Philippe GENDRE wrote: >> >>> 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 >> >> -- >> 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 |
Free forum by Nabble | Edit this page |