0

有人可以向我解释一下我在下面的代码中做错了什么吗?

我正在使用 executeJavascript 方法向 Webview 发送一系列命令,我想遍历每个命令,然后在执行下一个命令之前等待任意时间。

当我运行它时实际发生的是,每次我在循环中暂停时应用程序都会挂起,然后一旦循环完成,我所有的 javascript 操作都会立即发生。我认为通过将我的 executeJavascript 包装到 Runlater 类中,它将与 Application 线程很好地同步......

我似乎在兜圈子,因此将不胜感激帮助/方向,谢谢。

我设置了三个类,A: Main.class 包含以下内容:

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

B:ScriptRunner.class 包含以下内容:

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 包含以下内容:

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;
}

}

4

1 回答 1

0

尝试在 Thread 中扩展您的 ScriptRunner 类

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();
            }
        }
}

然后打电话

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

我认为问题 Thread.sleep(3000);在于导致应用程序挂起。该进程应在​​ Thread 上运行。

于 2016-09-29T07:33:08.473 回答