问题:
今天在同一线程内多次使用QProcess调用mplayer播放视频,
每次调用完毕后都调用QProcess::terminate ()结束,但是线程下次循环调用QProcess时出现错误:
“QProcess::start: Process is already running”
分析:
terminate()只是通知QProcess结束,但QProcess并没有马上结束;QProcess文档:http://developer.qt.nokia.com/doc/qt-4.8/qprocess.html
void QProcess::kill () [slot]
Kills the current process, causing it to exit immediately.
On Windows, kill() uses TerminateProcess, and on Unix and Mac OS X, the SIGKILL signal is sent to the process.
On Symbian, this function requires platform security capability PowerMgmt. If absent, the process will panic with KERN-EXEC 46.
Note: Killing running processes from other processes will typically cause a panic in Symbian due to platform security.
See also Symbian Platform Security Requirements and terminate().
void QProcess::terminate () [slot]
Attempts to terminate the process.
The process may not exit as a result of calling this function (it is given the chance to prompt the user for any unsaved files, etc).
On Windows, terminate() posts a WM_CLOSE message to all toplevel windows of the process and then to the main thread of the process itself. On Unix and Mac OS X the SIGTERM signal is sent.
Console applications on Windows that do not run an event loop, or whose event loop does not handle the WM_CLOSE message, can only be terminated by calling kill().
On Symbian, this function requires platform security capability PowerMgmt. If absent, the process will panic with KERN-EXEC 46.
Note: Terminating running processes from other processes will typically cause a panic in Symbian due to platform security.
See also Symbian Platform Security Requirements and kill().
结论:
如创建了一个线程 mplayerProcess = new QProcess(this);
使用线程打开另外一个程序 mplayerProcess->start(tr(paths, args);
当被调用的程序退出时,一定要加这个判断:
if(!mplayerProcess->waitForFinished(3000))
{......}
否则你再接着使用该线程时,可能会提示:QProcess::start: Process is already running 。