1. 程式人生 > >使用QProcess要注意的問題(QProcess::start: Process is already running)

使用QProcess要注意的問題(QProcess::start: Process is already running)

問題:

今天在同一執行緒內多次使用QProcess呼叫mplayer播放視訊,
每次呼叫完畢後都呼叫:: ()結束,但是執行緒下次迴圈呼叫出現錯誤:
“QProcess::start: Process is already running”

分析:

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.

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.

結論:

如建立了一個執行緒   mplayerProcess = new QProcess(this);
使用執行緒開啟另外一個程式  mplayerProcess->start(tr(paths, args);
當被呼叫的程式退出時,一定要加這個判斷:

if(!mplayerProcess->waitForFinished(3000))

{......}

否則你再接著使用該執行緒時,可能會提示:QProcess::start: Process is already running 。