IJ.showProgress in java


#1

Hello,

I’ve got a question relation to the IJ.showProgress() function.
I am using it a computationally expensive for-loop in a java plugin and it does not show the progress.

I found this:


There it is mentioned that one needs to create different threads…?
Is that really necessary? And if so, could someone maybe show me an example of how to do this?


#2

Found out myself:

Thread t1 = new Thread(new Runnable() {
                        public void run() {
                           myFunction(...);
                        }
                    });
                    t1.start();

Where myFunction contains above mentioned for-loop


#3

And that solves your issue, i.e. shows the progress bar as expected?


I wonder if the IJ2 way of doing it (using StatusService) would work better for you:

import org.scijava.app.StatusService;

// ...

@Parameter
final StatusService sts;

// ...

sts.showProgress(current, total)

#4

Hi Jan,

My way works for me…however, depends a bit on the OS. Works even more reliably if the status update and the computation run in different threads. I did not try your suggestion yet, but thanks for the suggestion!


#5

you can use the following

final AtomicInteger progress = new AtomicInteger(0); // the progress to show

then

Thread t1 = new Thread(new Runnable() {
                        public void run() {
                           myFunction(..., progress);
                        }
                    });
                    t1.start();

and in MyFunction

  private void myFunction(..., AtomicInteger progress) {
    ...
    IJ.showProgress(progress.getAndIncrement(), total);
 }

#6

FWIW, the SciJava status service currently does not do any magic threading or batching of status events. However, SciJava commands already run on their own thread. On my system, the following script works as expected:

// @StatusService ss
max = 1000
for (i=0; i<max; i++) {
	ss.showStatus(i, max, "Processing " + i + "/" + max)
	Thread.sleep(10)
}
ss.clearStatus()

@Christian_Tischer What kind of plugin are you using? A SciJava Command? Or a legacy ImageJ 1.x PlugIn? If the latter, try the former instead.


#7

@ctrueden

Thanks for the code!

As I am using the Swing GUI anyway I am doing now all my progress logging via the invokeLater function, e.g. like this:

public static void threadLog(final String log) {
    SwingUtilities.invokeLater(new Runnable() {
        public void run() {
            IJ.log(log);
        }
    });
} 

That works OK for me.


#8

That is fine—it just ties you to the legacy ImageJ 1.x codebase, as well as Swing UIs in particular. In other words, it is not flexible, and not future-proof. But it gets the job done for your narrower scope.