progress bar is not updating correctly

1

I am developing a data synchronizer, where every 5 seconds I check if there is data to be synchronized. The synchronization part with the check time every 5 seconds is already working. However, I was unable to make the progress bar follow the progress of the Task, that is, go from 0 to 100 at each iteration of the loop. Here is my code:

 public void timerSincronizacao05Secs() {
    Task sincronizacaoTask05Secs = new Task<Void>() {
        @Override
        protected Void call() throws Exception {
            while (isRunning == false) {
                isRunning = true;
                System.out.println("iniciou  em.....: " + new Date());
                fachada.sincronizarProdutos();
                barraProgresso.setProgress(0.50);
                fachada.sincronizarClientes();
                barraProgresso.setProgress(barraProgresso.getProgress() + 0.50);
                System.out.println("terminou em.....: " + new Date());
                isRunning = false;
                try {
                    Thread.sleep(5000);
                } catch (InterruptedException ex) {
                    break;
                }
            }
            return null;
        }
    };

    lblteste.textProperty().bind(sincronizacaoTask.messageProperty());
    Thread threadSinv05Secs = new Thread(sincronizacaoTask05Secs);
    threadSinv05Secs.setName("Thread sincronização a cada 5 segundos");
    threadSinv05Secs.setDaemon(true);
    threadSinv05Secs.start();
}

I know it's not the best way to do it, but if anyone can help, thank you right away!

    
asked by anonymous 25.08.2017 / 14:21

1 answer

1

The problem is that you binded the messageProperty to the thread without using the update methods that the thread class offers: updateMessage() , updateProgress() , updateTitle() , updateValue() .

The correct way to connect Thread's progress to the progress of the toolbar is as follows:

ProgressBar progressbar = new ProgressBar(0.0);

// [...] Algum código

Task<Void> task = new Task<Void>() {
    @Override
    protected Void call() throws Exception {
        // Método isCancelled() retorna true quando task.cancel() é chamado
        while(!isCancelled()){
            for (int i = 0; i < 10000000; i++){
                // updateProgress(workdone,totalwork) valor numérico
                // para trabalho já realizado e trabalho total 
                updateProgress(i, 10000000);
            }
            Thread.sleep(5000);
        }
        return null;
    }
};
progressbar.progressProperty().bind(task.progressProperty());

The code was not even supposed to work since changes to the GUI can only be made by the FX Thread (perhaps because of this the bug). Your code could look like this:

updateMessage("iniciou  em.....: " + new Date());
fachada.sincronizarProdutos();
updateProgress(50,100);
fachada.sincronizarClientes();
updateProgress(100,100);
updateMessage("terminou em.....: " + new Date());

// [...] 
lblteste.textProperty().bind(sincronizacaoTask.messageProperty());
progressbar.progressProperty().bind(sincronizacaoTask.progressProperty());
    
26.08.2017 / 18:13