| 
| SubMonitor usage in deep recursion chains [message #335056] | Thu, 19 March 2009 14:34  |  | 
| Eclipse User  |  |  |  |  | In the API, it is said that the SubMonitor is more efficient in deep recursion chains.
 
 Does somebody know how to correctly report progress in recursive code
 combined with loops and conditions?
 
 Below is an example of what I am doing right now. It has been simplified
 to show only the behavior. I dont what I am doing wrong but I don't have
 any progress.
 
 void method1(IProgressMonitor monitor){
 SubMonitor parent = SubMonitor.convert(monitor, 1);
 
 method2(parent);
 
 if(monitor != null){
 monitor.done();
 }
 }
 
 void method2(SubMonitor monitor){
 int count2 = 10;
 monitor.setWorkRemaining(count2);
 for(int i=0; i<count2; i++){
 if(condition){
 method3(monitor.newChild(1));
 }
 monitor.setWorkRemaining(--count2);
 }
 }
 
 void method3(SubMonitor monitor){
 int count3 = 13;
 monitor.setWorkRemaining(count3);
 for(int i=0; i<count3; i++){
 if(condition){
 method2(monitor.newChild(1));
 }
 monitor.setWorkRemaining(--count3);
 }
 }
 
 This code reports the messages in each method but the  progress is not
 visible on the bar.
 
 Thanks for any suggestions.
 |  |  |  | 
| 
| Re: SubMonitor usage in deep recursion chains [message #335080 is a reply to message #335056] | Fri, 20 March 2009 10:01  |  | 
| Eclipse User  |  |  |  |  | Hi Aboubacar, see below.
 
 Aboubacar Kaba wrote:
 > In the API, it is said that the SubMonitor is more efficient in deep
 > recursion chains.
 >
 > Does somebody know how to correctly report progress in recursive code
 > combined with loops and conditions?
 >
 > Below is an example of what I am doing right now. It has been simplified
 > to show only the behavior. I dont what I am doing wrong but I don't have
 > any progress.
 > void method1(IProgressMonitor monitor){
 > SubMonitor parent = SubMonitor.convert(monitor, 1);
 >
 > method2(parent);
 You should pass the monitor with newChild(1)
 >
 > if(monitor != null){
 >   monitor.done();
 > }
 Don't call done() on the given monitor. See the javadoc of SubMonitor.
 
 > }
 >
 > void method2(SubMonitor monitor){
 You should create a local SubMonitor as you did above.
 
 > int count2 = 10;
 > monitor.setWorkRemaining(count2);
 > for(int i=0; i<count2; i++){
 >   if(condition){
 >    method3(monitor.newChild(1));
 >   }
 >   monitor.setWorkRemaining(--count2);
 > }
 > }
 >
 > void method3(SubMonitor monitor){
 Same here, you need a local SubMonitor. This way, the calling method
 does not have to know how much work is needed.
 > int count3 = 13;
 > monitor.setWorkRemaining(count3);
 > for(int i=0; i<count3; i++){
 >   if(condition){
 >    method2(monitor.newChild(1));
 >   }
 >   monitor.setWorkRemaining(--count3);
 > }
 > }
 >
 
 Regards, Sebastian
 |  |  |  | 
Powered by 
FUDForum. Page generated in 0.03491 seconds