{
Data data;
void tick();
public slots:
dataIn(Data);
}
WorkerThread::WorkerThread()
{
moveToThread(this); // optional
}
// This is the callback function of the windows media timer.
// It just calls tick() of my worker thread.
void CALLBACK PeriodicCallback(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
WorkerThread* wt = (WorkerThread*)dwUser;
wt->tick();
}
// When the thread is started, I create the MM timer and have it call PeriodicCallback().
void RobotControl::start()
{
// Set resolution to the minimum supported by the system.
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
timerRes = qMin(qMax(tc.wPeriodMin, (UINT) 0), tc.wPeriodMax);
timeBeginPeriod(timerRes);
// Create the callback timer.
timerId = timeSetEvent(12, 0, PeriodicCallback, (DWORD_PTR) this, 1);
}
WorkerThread::tick()
{
// Initialize and call the event loop.
static bool eventLoopInitialized = false;
if (!eventLoopInitialized)
{
eventLoopInitialized = true;
}
else
{
//QApplication::processEvents();
eventLoop->processEvents(); // not sure which one to call
}
use(data);
qDebug
() <<
QThread::currentThreadId() <<
"worker thread was ticked";
}
// I would like this slot to be handled on the same thread as tick().
WorkerThread::dataIn(Data d)
{
data = d;
qDebug
() <<
QThread::currentThreadId() <<
"data received";
}
class WorkerThread : QThread
{
QEventLoop* eventLoop; // my custom event loop
Data data;
void tick();
public slots:
dataIn(Data);
}
WorkerThread::WorkerThread()
{
moveToThread(this); // optional
}
// This is the callback function of the windows media timer.
// It just calls tick() of my worker thread.
void CALLBACK PeriodicCallback(UINT uID, UINT uMsg, DWORD dwUser, DWORD dw1, DWORD dw2)
{
WorkerThread* wt = (WorkerThread*)dwUser;
wt->tick();
}
// When the thread is started, I create the MM timer and have it call PeriodicCallback().
void RobotControl::start()
{
// Set resolution to the minimum supported by the system.
TIMECAPS tc;
timeGetDevCaps(&tc, sizeof(TIMECAPS));
timerRes = qMin(qMax(tc.wPeriodMin, (UINT) 0), tc.wPeriodMax);
timeBeginPeriod(timerRes);
// Create the callback timer.
timerId = timeSetEvent(12, 0, PeriodicCallback, (DWORD_PTR) this, 1);
}
WorkerThread::tick()
{
// Initialize and call the event loop.
static bool eventLoopInitialized = false;
if (!eventLoopInitialized)
{
eventLoopInitialized = true;
eventLoop = new QEventLoop();
}
else
{
//QApplication::processEvents();
eventLoop->processEvents(); // not sure which one to call
}
use(data);
qDebug() << QThread::currentThreadId() << "worker thread was ticked";
}
// I would like this slot to be handled on the same thread as tick().
WorkerThread::dataIn(Data d)
{
data = d;
qDebug() << QThread::currentThreadId() << "data received";
}
To copy to clipboard, switch view to plain text mode
Bookmarks