However, if the task was canceled, a return value from the call method is ignored. Inside the call method, you can use the updateProgress, updateMessage, updateTitle methods, which update the values of the corresponding properties on the JavaFX Application thread. On the other hand, the Task class is designed to be used with JavaFX GUI applications, and it ensures that any changes to public properties, change notifications for errors or cancellation, event handlers, and states occur on the JavaFX Application thread. For example, manipulating an active scene graph from the call method throws runtime exceptions. The call method is invoked on the background thread, therefore this method can only manipulate states that are safe to read and write from a background thread. Your implementation of the Task class must override the call method to do the background work and return the result. First, you need to extend the Task class. Tasks are used to implement the logic of work that needs to be done on a background thread.
The progress of the work being done by the Worker object can be obtained through three different properties such as totalWork, workDone, and progress.įor more information on the range of the parameter values, see the API documentation. At any time before the end of the Worker object the developer can interrupt it by invoking the cancel method, which puts the Worker object into the CANCELLED state.ĭistinctions in the lifecycle of a ScheduledService object can be found in the The ScheduledService Class section. Otherwise, if any exceptions are thrown during the execution of the Worker object, its state becomes FAILED and the exception property is set to the type of the exception that occurred. The state of a Worker object that completes successfully is SUCCEEDED, and the value property is set to the result of this Worker object. Note that even when the Worker object is immediately started without being scheduled, it first transitions to the SCHEDULED state and then to the RUNNING state. After that, when the Worker object is performing the work, its state becomes RUNNING. Upon being scheduled for work, the Worker object transitions to the SCHEDULED state. When created, the Worker object is in the READY state. The lifecycle of the Worker object is defined as follows. The state of the Worker object is observable and usable from the JavaFX Application thread. The Worker interface defines an object that performs some work on one or more background threads.
#Force filebot update sample code#
Note that at some point you must communicate with the JavaFX Application thread, either with a result or with the progress of the background task.įor the most cases and for the majority of developers the recommended way is to use the JavaFX APIs provided by the ncurrent package, which takes care of multithreaded code that interacts with the UI and ensures that this interaction happens on the correct thread.
If you have special requirements or need extra power over the code, implementing a background worker by creating a Runnable object and a new thread is an appropriate way to go. A best practice is to do these tasks on one or more background threads and let the JavaFX Application thread process user events. Implementing long-running tasks on the JavaFX Application thread inevitably makes an application UI unresponsive. The JavaFX scene graph, which represents the graphical user interface of a JavaFX application, is not thread-safe and can only be accessed and modified from the UI thread also known as the JavaFX Application thread.