我想在我的QT應用程序中實現超時。我已經使用QThread來執行操作(需要超時的任務),並使用QElapsedTimer來計算等待執行操作的已用時間。下面是代碼片段停止QThread執行
QElapsedTimer timeoutTimer; // Timer to count the elapsed time waiting for the operation to complete.
long timeoutInterval=10000
MyThread mThread(); // QThread implementation
timeoutTimer.start();
mThread.start();
while((timeoutTimer.elapsed() < timeoutInterval) && mThread.isRunning()){
sleep(5);
}
if(mThread.isRunning()){
mThread.terminate();
}
現在,如果沒有完成任務,併發生超時,我得到「摧毀,線程仍在運行」和應用程序時墜毀。 我試着調用QThread的terminate()函數,但是它在Windows上工作,但是在Linux上我得到了段錯誤。
我試過'quit()'而不是'terminate',但它沒有工作。 – Vinod