2

I am making a program to run minecraft and I have a problem - i need to stop the task when user clicks the button.

Here is my code:

public void onStart(ActionEvent actionEvent) {
    Task<Void> task = new Task<Void>() {
        @Override
        protected Void call() {
            RunMinecraft.runMinecraft(null);
            return null;
        }

    };

    new Thread(task).start();
}

RunMinecraft.class:

public class RunMinecraft {

    public static void runMinecraft(String server) {

        //Here processes:
        //Auth user
        //Check all files
        //Download all needed files
        //Run Game

    }
}

I also tried to create a new boolean variable and check it in runMinecraft method after each process and if this (boolean == false), I stopped the method. But this case is not good.

I also tried this code: JavaFX - Cancel Task doesn't work But my task didn't stop.

Thank you in advance.

VENTO
  • 43
  • 1
  • 8
  • Canceling a task will also set the interrupt status on the thread on which it is running. So if your `runMinecraft(...)` method handles interrupts gracefully, then your cancel should work. See this [old, but excellent, article](https://www.ibm.com/developerworks/library/j-jtp05236/). – James_D Jan 26 '18 at 14:08
  • http://www.java67.com/2015/07/how-to-stop-thread-in-java-example.html – SedJ601 Jan 26 '18 at 14:16
  • 2
    Note though that stopping a thread by setting and polling a flag will not let the task enter the `CANCELLED` state; cancellation/interruption may be a better approach if you want to use that part of the `javafx.concurrent` API. – James_D Jan 26 '18 at 14:33

0 Answers0