1

I wish to Show progress of a long running operation(process) in UI, so that the user can understand the status of the background job. This is the way I have implemented and I feel like the code is absurd. Below is my code

dialog.run(true,false, new IRunnableWithProgress() {
    @Override
    public void run(IProgressMonitor monitor) throws InvocationTargetException, InterruptedException {
        monitor.beginTask("Main process", 10);
        for (int i = 0; i < 10; i++) {
            if (monitor.isCanceled()) return;
            monitor.subTask("Status  message");
            sleep(1000);
            // worked increases the monitor, the values are added to the existing ones
            monitor.worked(1);
            if(i == 3) {
                sleep(3000);
                callMe();//calling a long running function 
            }
            if(i == 9) {
                monitor.subTask("finishing setup..... please wait ");
                sleep(2000);
            }
        }
        monitor.done();
    }
});

Note: There is a sleep method somewhere in the code

here at i == 3 an operation/function is called that takes a minimum of 5 minutes, post execution of the function the progress continues.

I don't want the progress to be stopped while executing the function(long running operation) rather progress must be shown even while executing it.

can someone show the correct programming practices in showing progress

Chandrayya G K
  • 8,719
  • 5
  • 40
  • 68
srk
  • 4,857
  • 12
  • 65
  • 109
  • What do you mean by _"the progress to be stopped ..."_? Stopped by the user by pressing the button next to the progress bar? – True Soft Aug 26 '11 at 13:52
  • I assume there are no feedback during the 5 minute operation? – Tonny Madsen Aug 26 '11 at 14:12
  • @True "the progress to be stopped" mean that some progress should be shown unlike keeping idle(no movement in progress) for 5minutes or else some message must be shown yes you are right there is no feedback during 5 minute operation in the above scenario but i want to view some messages of the task – srk Aug 26 '11 at 14:39
  • true and tony can you guide me – srk Aug 27 '11 at 12:13
  • 2
    Be aware that when you're off calling that other method the progress won't really be updating. It'll be stuck with the same status message and ignoring any cancel requests the entire 5 minutes. – nitind Aug 28 '11 at 12:59

1 Answers1

1

The reason your code feels absurd is that wrapping the long-running method in a IRunnableWithProgress.run() really does not add much in itself, there is no magic.

To make the ProgressMonitorDialog (or e.g. the related Job API) work for you, you need to change "callMe()" so it takes "IProgressMonitor monitor" as a parameter, and use that to listen for cancel-requests, and use it also for reporting progress.

To say the same again, using different wording: You need to recursively add "IProgressMonitor monitor" as a parameter to all long-running method calls. All long-running operations must be build with this (IProgressMonitor) in mind if any progress is to be reported and/or you want it to be cancelable.

Eirik W
  • 3,086
  • 26
  • 29