I'm trying to run a task every 1 minute. The task is executed however when it arrives in 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 the system time and checks to see if it is the same as the bank time, if the hours are the same an alert is displayed. It was all working before putting it in TimerTask, The alertaGeral();
method is called and it works since it is not with the same Hours, when the hours are equal the alert should be displayed but of 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);
}
}
});
}