Background tasks for long running queries on main window?


I have a “dashboard” page I developed using charts and KeyValueContainers on my application’s main window. Some of my queries can take a few minutes to load.

I am looking at various options to speed up the queries, but another solution is to bring the main window up immediately and put the query refresh and subsequent chart refresh in one or more background tasks.

Has anybody ever done this? Are there any examples out there I can look at?

Is there a better way to handle this?

Hello @ericraskin

I suggest you to use CUBA Background Tasks API. It seems that it’s what you need.


Thanks. Are there any examples I can see? The documentation doesn’t show a complete interaction with a screen, and the Live Demo button doesn’t link to any demos of BackgroundTasks that I can find.

Hello @ericraskin

Sorry for waiting. Please take a look at this example:

private BackgroundWorker backgroundWorker;
private Notifications notifications;

private void onAfterShow(AfterShowEvent event) {
    // instantiate a task
    BackgroundTask<Long, String> longRunningTask =
            new BackgroundTask<Long, String>(30L, TimeUnit.SECONDS, this) {

                public String run(TaskLifeCycle<Long> taskLifeCycle) throws Exception {
                    // "hard working"
                    return "Task finished!";

                public void done(String result) {
    // run the task


Thank you very much. :grinning: