gpt4 book ai didi

java - waitFor 命令不等待

转载 作者:行者123 更新时间:2023-12-02 06:35:28 26 4
gpt4 key购买 nike

我在进程上使用 waitFor 命令时遇到问题。我的代码是这样的

//preconditions
try{
// lock the work station
Process p= Runtime.getRuntime().exec("C:\\Windows\\System32\\rundll32.exe user32.dll,LockWorkStation");
int exitval=p.waitFor();
// If the authentication is successful
if(exitval==0)
{
//statements to insert into database
}
}
catch(IOException e)
{
e.printStackTrace();
}
catch (InterruptedException e) {
e.printStackTrace();
}

该进程可以很好地锁定屏幕,但在用户实际能够使用退出值“0”进行身份验证并且程序将语句插入我的数据库之前它就退出了。我希望该过程等到用户成功通过身份验证,然后将我的数据插入数据库。我用谷歌搜索了很多,但没有成功。我应该使用不同的进程来锁定屏幕吗?

最佳答案

在执行LockWorkStation时,会在幕后调用以下内容。请注意,它异步执行

BOOL WINAPI LockWorkStation(void);
If the function succeeds, the return value is nonzero. Because the function executes asynchronously,
a nonzero return value indicates that the operation has been initiated. It does not indicate whether
the workstation has been successfully locked.

此外,在上面的代码中,您需要执行该过程。

在您的问题中,更改:

Process p= Runtime.getRuntime().("C:\\Windows\\System32\\rundll32.exe user32.dll,LockWorkStation");
int exit = p.waitFor();

Process p= Runtime.getRuntime().exec("C:\\Windows\\System32\\rundll32.exe user32.dll,LockWorkStation");
int exit = p.waitFor();

此外,您可能想考虑使用 ProcessBuilder 而不是 Runtime.exec()

关于java - waitFor 命令不等待,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19706014/

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