Commit fac4f9d4c38946415dc5be1d53c097d4cc888057
1 parent
15f38d8f
Don't use the main thread's event loop to start processes
Due to Qt's limitations, it is necessary to have a consistent thread start and stop a given process, but that thread does not have to be the main thread rather, any thread in an event loop is fine.
Showing
1 changed file
with
27 additions
and
19 deletions
openbr/plugins/process.cpp
| ... | ... | @@ -25,13 +25,11 @@ public: |
| 25 | 25 | CommunicationManager() |
| 26 | 26 | { |
| 27 | 27 | basis = new QThread; |
| 28 | - basis->moveToThread(QCoreApplication::instance()->thread()); | |
| 29 | 28 | moveToThread(basis); |
| 30 | 29 | server.moveToThread(basis); |
| 31 | 30 | outbound.moveToThread(basis); |
| 32 | 31 | |
| 33 | 32 | // signals for our sever |
| 34 | - connect(this, SIGNAL(pulseEndThread()), basis, SLOT(quit() )); | |
| 35 | 33 | connect(&server, SIGNAL(newConnection()), this, SLOT(receivedConnection() )); |
| 36 | 34 | connect(this, SIGNAL(pulseStartServer(QString)), this, SLOT(startServerInternal(QString)), Qt::BlockingQueuedConnection); |
| 37 | 35 | connect(this, SIGNAL(pulseOutboundConnect(QString)), this, SLOT(startConnectInternal(QString) ), Qt::BlockingQueuedConnection); |
| ... | ... | @@ -62,7 +60,6 @@ public: |
| 62 | 60 | |
| 63 | 61 | void shutDownThread() |
| 64 | 62 | { |
| 65 | - emit pulseEndThread(); | |
| 66 | 63 | basis->quit(); |
| 67 | 64 | basis->wait(); |
| 68 | 65 | delete basis; |
| ... | ... | @@ -276,8 +273,6 @@ signals: |
| 276 | 273 | void pulseSendSerialized(); |
| 277 | 274 | void pulseShutdown(); |
| 278 | 275 | void pulseOutboundConnect(QString serverName); |
| 279 | - void pulseEndThread(); | |
| 280 | - | |
| 281 | 276 | |
| 282 | 277 | public: |
| 283 | 278 | QByteArray readArray; |
| ... | ... | @@ -448,28 +443,39 @@ class ProcessInterface : public QObject |
| 448 | 443 | { |
| 449 | 444 | Q_OBJECT |
| 450 | 445 | public: |
| 446 | + QThread *basis; | |
| 447 | + | |
| 448 | + ~ProcessInterface() | |
| 449 | + { | |
| 450 | + basis->quit(); | |
| 451 | + basis->wait(); | |
| 452 | + delete basis; | |
| 453 | + } | |
| 454 | + | |
| 451 | 455 | ProcessInterface() |
| 452 | 456 | { |
| 453 | - this->moveToThread(QCoreApplication::instance()->thread()); | |
| 454 | - workerProcess.moveToThread(QCoreApplication::instance()->thread()); | |
| 457 | + basis = new QThread(); | |
| 458 | + | |
| 459 | + moveToThread(basis); | |
| 460 | + workerProcess.moveToThread(basis); | |
| 461 | + | |
| 455 | 462 | connect(this, SIGNAL(pulseEnd()), this, SLOT(endProcessInternal()), Qt::BlockingQueuedConnection); |
| 456 | 463 | connect(this, SIGNAL(pulseStart(QStringList)), this, SLOT(startProcessInternal(QStringList)), Qt::BlockingQueuedConnection); |
| 464 | + | |
| 465 | + basis->start(); | |
| 457 | 466 | } |
| 467 | + | |
| 458 | 468 | QProcess workerProcess; |
| 459 | 469 | void endProcess() |
| 460 | 470 | { |
| 461 | - if (QThread::currentThread() != QCoreApplication::instance()->thread()) | |
| 462 | - emit pulseEnd(); | |
| 463 | - else | |
| 464 | - endProcessInternal(); | |
| 471 | + emit pulseEnd(); | |
| 465 | 472 | } |
| 473 | + | |
| 466 | 474 | void startProcess(QStringList arguments) |
| 467 | 475 | { |
| 468 | - if (QThread::currentThread() != QCoreApplication::instance()->thread() ) | |
| 469 | - emit pulseStart(arguments); | |
| 470 | - else | |
| 471 | - startProcessInternal(arguments); | |
| 476 | + emit pulseStart(arguments); | |
| 472 | 477 | } |
| 478 | + | |
| 473 | 479 | signals: |
| 474 | 480 | void pulseEnd(); |
| 475 | 481 | void pulseStart(QStringList); |
| ... | ... | @@ -500,10 +506,12 @@ struct ProcessData |
| 500 | 506 | |
| 501 | 507 | ~ProcessData() |
| 502 | 508 | { |
| 503 | - comm.sendSignal(CommunicationManager::SHOULD_END); | |
| 504 | - proc.endProcess(); | |
| 505 | - comm.shutdown(); | |
| 506 | - comm.shutDownThread(); | |
| 509 | + if (initialized) { | |
| 510 | + comm.sendSignal(CommunicationManager::SHOULD_END); | |
| 511 | + proc.endProcess(); | |
| 512 | + comm.shutdown(); | |
| 513 | + comm.shutDownThread(); | |
| 514 | + } | |
| 507 | 515 | } |
| 508 | 516 | }; |
| 509 | 517 | ... | ... |