Show progress text from a long-running service on the screen that calls it

I have a service that runs a very long process. It uses the Observer pattern to notify its caller with Strings detailing how things are going.

In the screen that calls this service I’d like to show these lines as they are reported. The problem is, once the service is invoked, the UI is locked, and the lines all spam-report when the service is done.

Is there a way around this?

Sure, use background tasks.

See the docs and online example.

The problem is that here the code that does the work is in a service, and once called it just does its work. I’d have to refactor the entire thing to work as a background task.

Is there another way?

I did manage to get this to work by using the suggested BackgroundTask/etc stuff. What I did was in the run() method, add taskLifecycle::publish to the service’s listeners. And then in the progress() method just concat onto a textArea the strings being sent.

It works. But the amount of throws InterruptedExceptions I had to add to method signatures all over the hierarchy is pretty epic!

But it works.