Refreshing screen using a while(condition) in each 5 seconds

I have a popup showing some statuses. This popup is opened from a screen where there is a run button
In popup in aftershow handler i added a while loop with a condition to repeat until the run action is completed and inside i wait 5 seconds each time. Code attached below. But what happens is the popup is shown only after the while loop exits.
I can see the REFRESHING logs but popup is not shown.
Is there any workaround for this

public void onAfterShow(AfterShowEvent event) {
    BatchStatus wholeBatchStatus;
    BatchStatusEnum wholeBatchStatusEnum = BatchStatusEnum.RUNNING;
    while(BatchStatusEnum.RUNNING.equals(wholeBatchStatusEnum)) {
        try {
            System.out.println("REFRESHING" + new Date());
            Thread.sleep(5000);
            batchStatusesDl.load();
        } catch (Exception e) {
        }
        wholeBatchStatus = dataManager.load(BatchStatus.class)
                .query("select e from BatchStatus e where e.source is null")
                .one();
        wholeBatchStatusEnum = wholeBatchStatus.getStatus();
    }
}

Hi

You get this behavior because onAfterShow method executed synchronously and UI won’t be updated until method exits.
If you need to perform some non-blocking operation please use background task. See documentation.