Reputation: 3
I am running into a problem while using waitFor command on a process. My code is like this
//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();
}
The process is locking the screen fine, but it is exiting before the user is actually able to authenticate with an exit value of '0' and the program is inserting the statements into my database. I want the process to wait until the user has been successfully authenticated and then insert my data into the database. I've googled for a quite a bit without any success. Should I use a different process to lock the screen?
Upvotes: 0
Views: 476
Reputation: 11841
Under the covers the following is being called when executing LockWorkStation
. Note, that it executes asynchronously
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.
Also, in your code above you need to execute the process.
In the presented in your question, change:
Process p= Runtime.getRuntime().("C:\\Windows\\System32\\rundll32.exe user32.dll,LockWorkStation");
int exit = p.waitFor();
to
Process p= Runtime.getRuntime().exec("C:\\Windows\\System32\\rundll32.exe user32.dll,LockWorkStation");
int exit = p.waitFor();
Also, you might want to look into using ProcessBuilder
instead of Runtime.exec()
Upvotes: 2
Reputation: 298113
LockWorkStation
is an asynchronous function. It will always return immediately not waiting for an unlock. When running C:\Windows\System32\rundll32.exe user32.dll,LockWorkStation
from the console you might even see the next command prompt shortly before the screen gets locked. In other words, this has nothing to do with Java and Process.waitFor()
.
Upvotes: 0