我正在尝试制作一个程序:
我遇到问题的部分是:等到程序退出。
这是我的代码:
// This class is a thread.
// The synchronized block and the sleep are here to avoid that the process starts multiple times at the same time.
Process process = null;
synchronized(startProcessLock) // startProcessLock is a static object.
{
Thread.sleep(1000);
String[] command = new String[] {"cmd", "/c", filePath};
process = Runtime.getRuntime().exec(command);
}
if(process != null && process.isAlive())
{
// Here, the file has been opened
fileOpened = true;
process.waitFor();
// Here, the file has (normally) been closed
fileOpened = false;
// Then : upload the file and exit the thread
}
此程序通常必须同时打开多个WORD文档(通常为3或4)。每个文档的这个线程的一个实例。 并且始终会出现此问题:即使用户未关闭WINWORD进程,process.waitFor()也始终快速返回!我不明白为什么。
我错过了什么,或做错了什么?我是Java中流程操作的初学者。
感谢。
编辑:process.waitFor()并不总是返回。它返回我同时打开的所有文件,除了一个。在该线程的一个实例中,它可以工作,并且仅在我关闭单词时返回。
编辑:该命令存在同样的问题:new String[] {"C:\\Program Files (x86)\\Microsoft Office\\root\\Office16\\WINWORD.EXE", filePath};