WaitForSingleObject, WaitForMultipleObjects, QEvent, QWaitCondition
-
Hi, I am stuck with the items mentioned below, and would appreciate any guidance.
My application requires the use of a DataReceiver object which executes in it's own thread.
The code blocks while waiting for an external event. This code is implemented and uses
WaitForSingleObject(hEvent, INFINITE) to wait for data events.Because the application requires that I am able to terminate the main code loop, I need
an additional event together with the use of WaitForMultipleObjects(hEvents, INFINITE).The following code snippet attempts to show what I am attempting, I have excluded #includes etc for brevity.
Please note that I have read the QT documentation, unfortunately my knowledge is lacking, which makes the brevity with which these topics are dealt with, very challenging.
At //// 1. I would like to know whether the OnExit() slot may somehow be used in a similar way to the use of the external event generated by the external device, and if so, how I hook this up. I have made an attempt to create a HANDLE as a function pointer, without success.
If it cannot be used in this manner, I would like to know how to set up a QEvent to achieve this.At //// 2. I have read that WaitForMultipleObjects can be replaced with QWaitCondition,
yet the notes and examples presented do not provide enough information.Any comments over and above what I have asked will be appreciated. Thank you.
/**************************************************************************************/ // DataReceiver Header class DataReceiver : public QObject { Q_OBJECT public: DataReceiver(HANDLE); ~DataReceiver(); public slots: void onStarted(); void onDataReceive(); void onExit(); signals: void commsReceiverMessage(const QString &message); private: HANDLE handle; QMutex mutex; bool running; }; /**************************************************************************************/ // DataReceiver Source // vars bool running = false; bool dataReceived = false; // slot to handle exit request void DataReceiver::onExit() { running = false; } // void DataReceiver::onStarted() { DWORD nCount = 2; running = true; hEvent[0] = CreateEvent(NULL, false, false, NULL); hEvent[1] = CreateEvent(NULL, false, false, NULL); // hook the external event mutex.lock(); SetEventNotification(external_device, external_event_mask, hEvents[0]); mutex.unlock(); //// 1. Attach an additional event. How can this be done for this implementation ? QWinEventNotifier external_event_notifier; QWinEventNotifier internal_event_notifier; connect(&external_event_notifier, &QWinEventNotifier::activated, this, &DataReceiver::onDataReceive); connect(&internal_event_notifier, &QWinEventNotifier::activated, this, &DataReceiver::onStop); external_event_notifier.setEnabled(true); internal_event_notifier.setEnabled(true); while (running == true) { //// 2. How can this be done using QWaitCondition ? WaitForMultipleObjects(nCount, hEvents, false, INFINITE); if (dataReceived == true) { dataReceived = false; emit dataReceiverMessage("onDataReceive triggered"); } } } // handles external data receive event void DataReceiver::onDataReceive() { dataReceived = true; } // handles stop signal void DataReceiver::onStop() { running = false; } /**************************************************************************************/