我有我一直在嘗試用一些粗糙的代碼:如何停止運行阻塞永久循環的QThread?
someserver.cpp(一GUI)
#include "server.h"
#include "ui_server.h"
Server::Server(QWidget *parent) :
QMainWindow(parent),
ui(new Ui::Server)
{
ui->setupUi(this);
}
Server::~Server()
{
delete ui;
}
void Server::onBtnStartClicked()
{
QThread worker;
worker.start(); // Start worker thread that goes into an infinite loop with a blocking call
}
void Server::onBtnExitClicked()
{
// How do I cleanly stop worker from running?
QApplication::quit();
}
worker.cpp
#include "worker.h"
Worker::Worker(QObject *parent) :
QThread(parent)
{
}
void Worker::run()
{
for (;;)
{
// a blocking IO call here like pipe, or msgrcv
// process data received
}
}
由於工作線程在運行永遠循環使用阻塞IO調用,我將如何構造它以便在GUI線程中按下停止按鈕時,工作線程乾淨地停止?
的問題是阻塞調用是System V消息隊列,所以我不認爲我可以使用像QIODevice這樣的設置回調函數。 – 2012-02-11 21:29:18
@CodingDistrict我對這些並不熟悉,但肯定有某種回調裝置用於何時準備好消息?然而,這工作,我會認爲你可以繼承'QLocalSocket'併爲其打包。 – 2012-02-11 21:33:34