QProcess::finished 監視進行是否結束


QProcess有一個signal叫做finished,具體定義如下:void QProcess::finished(int exitCode,QProcess::ExitStatus exitStatus) 當進程結束的時候,該signal會被發射出去。exitCode就是進程的退出碼,而exitStatus就是退出狀態。若在一個系統服務中,想保持另一個進程始終處於運行狀態(比如某server),那么就可以connect這個finished信號。詳細代碼如下:

 

 void keepProcessRunning()
 {
  QProcess* p = new QProcess();
   QObject::connect(p,&QProcess::started,[](){
  qDebug()<<"Notepad.exe started!";
 });
 //Keep restarting if notepad.exe is finished
   QObject::connect(p,static_cast<void(QProcess::*)(int,QProcess::ExitStatus)>(&QProcess::finished),
         [p](intexitCode,QProcess::ExitStatus exitStatus){
         qWarning()<<"Notepad.exe was terminated!"
           <<"Exit code:"<<exitCode<<"Exit status:"<<exitStatus;
     qWarning()<<"Notepad.exe will restart...";
    p->start();    });
   p->start("notepad.exe");

}

 

 

以上代碼中,先連接了started() signal,但這僅僅是為了打印一條語句“Notepad.exe started!”。然后連接了finished signal,其主要動作是再次調用p->start().但是注意,這里的start()是不帶參數的。在兩次connect結束之后,調用p->start("notepad.exe");以啟動筆記本程序。connect中的p->start()之所以沒有參數,就是因為在它后面調用的start是有參數的。而再次start的話,是可以不再注明參數的。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM