Error running Timertask Javafx

Asked

Viewed 138 times

1

I am trying to perform a task every 1 minute. The task is executed but when it arrives at the right minute to perform the action of this exception:

java.lang.Illegalstateexception: Not on FX application thread; currentThread = Timer-0

In short, I have a method that takes system time and checks if it equals bank time, if the hours are equal an alert is displayed. It was all working before I put in Timertask, The Method alertaGeral(); is called and works since it is not with equal Hours, when the hours are equal the alert should be displayed but the error.

My method:

 private void executarTarefa() {
        //****INICIA A TAREFA ELE VERIFICA A CADA UM MINUTO****//  
        Platform.runLater(new Runnable() {
        @Override
        public void run() {
         SimpleDateFormat sdf = new SimpleDateFormat("dd/MM/yyyy hh:mm");
        System.out.println("Iniciado!");

        Timer timer = null;
        if (timer == null) {
            timer = new Timer();
            TimerTask tarefa = new TimerTask() {
                @Override
                public void run() {
                    try {
                        alertaGeral();
                        System.out.println("Alertado..");
                        System.out.println(sdf.format(new Date()));

                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            };
            timer.scheduleAtFixedRate(tarefa, TEMPO, TEMPO);
        }

        }
   });

    }
  • Everything you put into Timertask runs in a parallel thread, you’re probably trying to use some object that’s being used by the main thread. You have to stop using this object or create a copy of it and use it.

  • I’m trying to use a Lista<Tarefas>. I use the same list to fill my table

  • So I don’t think you can use the same list, because threads are competitors. Try to make with a copy, but any changes made to this list or its items will not be reflected in the original.

  • where is your method executarTarefa() and your table?

  • I think I found the error, I’m going to do a test. The error may be on this line:Alert dialogoAviso = new Alert(Alert.AlertType.INFORMATION);&#xA; dialogoAviso.setTitle("AVISO1");&#xA; dialogoAviso.setHeaderText("Voce tem um compromisso marcado para: " + c.getDataExibicao() + " as " + c.getHoraInicial());&#xA; dialogoAviso.setContentText("Compromisso: " + c.getCompromisso() + "\nNome do Beneficiário: " + c.getNomeBeneficiario());&#xA; dialogoAviso.showAndWait();

  • I’m using this Alert within the method alertaGeral(); and the method executaTarefa is in the builder

Show 1 more comment
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.