3

I have task in my app, and i do not know how it works return from this task.

public class TimeManager extends Service<String> {
@Override
protected Task<String> createTask() {
    return new Task<String>() {
        @Override
        protected String call() throws Exception {
            String txt = null;
            while (!isCancelled()) {
                    try {
                        txt = "some txt";
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }
                }
            return txt;
        }
    };
}

And in Main Class:

TimeManager time = new TimeManager();
time.start();
time.getValue();

time allways return null. What do I have to do to return the value? Thread works good and I can send data from the thread to the application

Light
  • 175
  • 1
  • 2
  • 11
  • 2
    You should retrieve the service value only when the service is [suceeded](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Service.html#setOnSucceeded-javafx.event.EventHandler-). You could also execute a particular process if the task faild with the [failed event](https://docs.oracle.com/javase/8/javafx/api/javafx/concurrent/Service.html#setOnFailed-javafx.event.EventHandler-) – Pagbo Jul 31 '18 at 08:26
  • that is, download online value can only be from the thread to the application never vice versa ? – Light Jul 31 '18 at 08:31

1 Answers1

6

Your task does not publish intermediate updates. Furthermore Service is used to run tasks in background threads to avoid blocking the JavaFX application thread. For this reason the value is likely to not be assigned if you access it directly after starting the service. It would be better to use a binding or a listener to the value property to retrieve the data when it's assigned.

public class TimeManager extends Service<String> {

    @Override
    protected Task<String> createTask() {
        return new Task<String>() {

            int i = 0;

            @Override
            protected String call() throws Exception {
                String txt = null;
                while (!isCancelled()) {
                    txt = Integer.toString(++i);
                    updateValue(txt); // publish new value
                    try {
                        Thread.sleep(1000);
                    } catch (InterruptedException e) {
                        System.out.println(e.getMessage());
                    }
                }
                return txt;
            }
        };
    }
}
TimeManager time = new TimeManager();
label.textProperty().bind(time.valueProperty());
time.start();

Note that in this case a service may not be required since you're running only a single task. Running the Task instance using new Thread(task).start() may actually suffice.

Furthermore there are better options for scheduling fast repeating updates of the GUI, see JavaFX periodic background task

fabian
  • 80,457
  • 12
  • 86
  • 114