gpt4 book ai didi

java - 单独的线程不会运行,阻塞主 UI

转载 作者:行者123 更新时间:2023-12-02 10:29:23 25 4
gpt4 key购买 nike

我有一个执行一组 PowerShell 命令的程序。现在的问题是,当用户单击某个选项并且执行命令的过程开始时,我需要更新标签的状态并显示图标。如果我只是在 try-catch 语句之前键入命令,它们将在整个 try-catch 语句结束后执行。因此,我尝试在下面提供的代码中的单独线程上运行它们,但在这种情况下,它们甚至没有被执行,只是 try-catch block 。 NORMAL_MODE() 方法由鼠标点击触发。

@FXML
private void NORMAL_MODE(){
new Thread(new Runnable() {
@Override
public void run() {
LABEL.setText("123");
ICON.setVisible(true);
}
}).start();
try (PowerShell powerShell = PowerShell.openSession()) {
PowerShellResponse response = powerShell.executeCommand("Set-Location “C:\\Windows\\Temp”");
response = powerShell.executeCommand("Remove-Item * -whatif -recurse -force");
System.out.println(response.getCommandOutput());
response = powerShell.executeCommand("Set-Location “C:\\Windows\\Prefetch”");
response = powerShell.executeCommand("Remove-Item * -whatif -recurse -force");
System.out.println(response.getCommandOutput());
response = powerShell.executeCommand("Set-Location “C:\\Documents and Settings”");
response = powerShell.executeCommand("Remove-Item * -whatif -recurse -force");
System.out.println(response.getCommandOutput());
response = powerShell.executeCommand("Set-Location $env:TEMP");
System.out.println(response.getCommandOutput());
response = powerShell.executeCommand("Remove-Item -whatif -Recurse -Force");
System.out.println(response.getCommandOutput());
} catch(PowerShellNotAvailableException ex) {
}
}

最佳答案

所有 JavaFX 事件处理程序都在 JavaFX 应用程序线程上运行,并且这些处理程序需要快速返回,而 PowerShell 不会发生这种情况。对 GUI 的任何修改都需要在 JavaFX 应用程序线程上进行。

看来您刚刚将错误的代码移至后台线程。

Task class可以帮助处理在后台线程上执行的代码的完成/异常。

@FXML
private void NORMAL_MODE(){
LABEL.setText("123");
ICON.setVisible(true);

Task<Void> task = new Task<Void>() {
@Override
protected Void call() throws Exception {
try (PowerShell powerShell = PowerShell.openSession()) {
PowerShellResponse response = powerShell.executeCommand("Set-Location “C:\\Windows\\Temp”");
response = powerShell.executeCommand("Remove-Item * -whatif -recurse -force");
System.out.println(response.getCommandOutput());
response = powerShell.executeCommand("Set-Location “C:\\Windows\\Prefetch”");
response = powerShell.executeCommand("Remove-Item * -whatif -recurse -force");
System.out.println(response.getCommandOutput());
response = powerShell.executeCommand("Set-Location “C:\\Documents and Settings”");
response = powerShell.executeCommand("Remove-Item * -whatif -recurse -force");
System.out.println(response.getCommandOutput());
response = powerShell.executeCommand("Set-Location $env:TEMP");
System.out.println(response.getCommandOutput());
response = powerShell.executeCommand("Remove-Item -whatif -Recurse -Force");
System.out.println(response.getCommandOutput());
}
return null;
}

};
task.setOnSucceeded(event -> {
// todo: update gui to indicate success
});
task.setOnFailed(event -> {
// todo: update gui to indicate an exception happened
});

new Thread(task).start();
}

onSucceededonFailed 事件处理程序在 JavaFX 应用程序线程上执行,因此您不需要使用 Platform.runLater对这些处理程序中完成的 GUI 的修改。

关于java - 单独的线程不会运行,阻塞主 UI,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53697861/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com