0

Can someone please explain to me what I am doing wrong with the below code?

I am using the executeJavascript method to send a series of commands to the Webview, I want to loop through each command and then wait an arbitrary amount of time before the next command is executed.

What actually happens when I run this is that the application will hang every-time I pause in the loop, then once the loop is complete all my javascript actions happen at once. I thought by wrapping my executeJavascript into the Runlater class that it would all be synced nicely with the Application thread...

I seem to be going round in circles so help/direction would be appreciated, thanks.

I have set up three classes, A: Main.class that contains the following:

 ...scene.setOnKeyPressed(event -> {
        switch (event.getCode()) {
            case SPACE:
                scriptRunner.run();
            case SHIFT:

B: ScriptRunner.class that contains the following:

public class ScriptRunner extends Task<Void> {

@Override
protected Void call() throws Exception {

    printOut("Running Test");
    try (InputStream fileInputStream = new FileInputStream("test.txt");
         InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, Charset.forName("UTF-8"));
         BufferedReader bufferedReader = new BufferedReader(inputStreamReader);) {
        String getCurrentLine;
        StepRunner stepRunner = new StepRunner();

        while ((getCurrentLine = bufferedReader.readLine()) != null) {
            final String currentLine = getCurrentLine;
            Platform.runLater(new Runnable() {
                @Override
                public void run() {
                    stepRunner.runStep(currentLine);
                }
            });
            Thread.sleep(3000);
        }

        printOut("Test finished");
        bufferedReader.close();
    } catch (
            IOException e) {
        e.printStackTrace();
    }
    return null;
}

C: StepRunner.class that contains the following:

public class StepRunner extends Task<Void> {
private String currentCommand;

public StepRunner (String currentCommand){
    this.currentCommand = currentCommand;
}

@Override
protected Void call() throws Exception {
    printOut("Got Here with " + currentCommand);
    WebEngine.executeJavascript(currentCommand);
    return null;
}

}

Ben Simms
  • 41
  • 1
  • 5

1 Answers1

0

Try to extend your ScriptRunner class in Thread

public class ScriptRunner extends Thread {

        @Override
        public void run() {
            printOut("Running Test");
            try (InputStream fileInputStream = new FileInputStream("test.txt");
                 InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream, Charset.forName("UTF-8"));
                 BufferedReader bufferedReader = new BufferedReader(inputStreamReader);) {
                String getCurrentLine;
                StepRunner stepRunner = new StepRunner();

                while ((getCurrentLine = bufferedReader.readLine()) != null) {
                    final String currentLine = getCurrentLine;
                    Platform.runLater(new Runnable() {
                        @Override
                        public void run() {
                            stepRunner.runStep(currentLine);
                        }
                    });
                    Thread.sleep(3000);
                }

                printOut("Test finished");
                bufferedReader.close();
            } catch (
                    IOException e) {
                e.printStackTrace();
            }
        }
}

then to call

Thread scriptRunner = new Thread(new ScriptRunner());
scriptRunner.run();

I think the problem is Thread.sleep(3000); that cause the app to hang. The process should be run on Thread.

  • Thanks, but even after extending my ScriptRunner class by a Thread and using the sleep inherited from it my Javascript commands executed on the page are still batched up. I think you're right and its the Thread that is causing the app to hang, just not sure how I can get around it. – Ben Simms Oct 02 '16 at 09:20