Pattern for progress status reporting

2019-05-08 16:03发布

问题:

I am implementing long running processes that require to a process bar - or progress percentage - to be displayed. The overall logic of the long running process is complex (various paged data retrieval), and as a result, I end up hardcoding lots of percentages in various places within the code.

What are considered as the best design patterns when it comes to update a completion percentage?

回答1:

I've found the pattern used in JFace around IProgressMonitor to work out pretty nicely. You might gain some insight by having a look at the class. Additional classes to look at are ProgressMonitorDialog and IRunnableWithProgress. You may also find this article of some help.



回答2:

If you know in advance the 'length' of the task you're going to perform, and have a way of knowing how far along you are, you should let the bar determine the percentage. For example, if you have to process a known number of files, give this number to the progress bar on construction. Then, during the process either the bar can poll the process, or the process can tell the bar how far along it is (in number of files). The bar can compute the percentage from these two values. This is how the standard progress bar in Java works.