views:

215

answers:

3

When a long-running process is being executed, it is a good practice to provide feedback to the user, for example, updating a progress bar.

Some FAQs for GUI libraries suggest something like this:

function long_running_progress()
    do_some_work()
    update_progress_bar()
    while finish
        do_some_work()
        update_progress_bar()
    end while
end function

Anyway, we know it is a best practice to separate business logic code from user interface code. The example above is mixing user interface code inside a business logic function.

What is a good technique to implement functions in the business logic layer whose progress could be easily tracked by an user interface without mixing layers?

Answers for any language or platform are welcome.

+1  A: 

If you used a MVC paradigm you could have the Model publish its current progress state as a property, the Controller could extract this every x seconds and then put it into the view. This assumes multi-threading though, which I'm not sure if you allow.

vanja.
+1  A: 

Publishing is a great way to go. It all depends on the platform how this is done. However, when it comes to the user experience there are a couple of things to consider as well:

  • Don't give the user a progress bar if you don't know how long the task is executing. What time is left? What does half-way mean? It's better to use hour-glass functionality (spinning wheels, bouncing progress bars, etc).

  • The only interesting thing to view progress on is time; what does Half-way in a process mean? You want to know if you got time for that cup of coffee. If you show other things you are probably displaying the workings of the system programming. Most users are not interested or just get confused.

  • Long running progress should always support the user with an escape, a way to cancel the request. You don't want to lock up the user for long time. Better still is to handle a long running request completely in the background and let the user get back when the result is back.

haqwin
+5  A: 

Provide a callback interface. The business logic will call its method every once in a while. The user layer will update the progress or whatever. If you want to allow cancellation – no problem, let the callback method have a return value which will indicate a need for cancellation. This will work regardless of number of threads.

sharptooth