[Interest] Difficulty running Timer from QThread
Israel Brewster
ijbrewster at alaska.edu
Mon Jul 19 17:48:24 CEST 2021
Thank you for the pointer to use QThread::currentThread for debugging! The problem was a bit more obscure than I was thinking, but that tip led me to the solution: the function that *called* the function which tried to start the timer was a VLC callback. Apparently, that callback was running in a different thread, so when it called my function *directly*, my function was also running in a different thread. By having the callback instead emit a signal which I connected to my function, my function ran in the correct thread, and I was able to start the timer properly (hopefully that all makes sense).
Thanks again!
---
Israel Brewster
Software Engineer
Alaska Volcano Observatory
Geophysical Institute - UAF
2156 Koyukuk Drive
Fairbanks AK 99775-7320
Work: 907-474-5172
cell: 907-328-9145
> On Jul 18, 2021, at 8:48 PM, Nuno Santos <nuno.santos at imaginando.pt> wrote:
>
> Israel,
>
> Maybe the problem is that fact that you call worker initVLC(videoWin) from the main thread.
>
> Maybe you could connect a signal to the thread started signal to call initVLC.
>
> Try to debug QThread::currentThread() when initVLC is called and when worker is created to see if what I say makes sense.
>
> Best,
>
> Nuno
>
>> On 19 Jul 2021, at 04:29, Israel Brewster <ijbrewster at alaska.edu <mailto:ijbrewster at alaska.edu>> wrote:
>>
>> Yes, this is a FAQ, but I can’t seem to figure out what I am doing wrong. I am using the worker pattern of QThread, (as opposed to subclassing), running Qt 5.15.2. In my controller’s constructor, I have the following code to initialize a worker:
>>
>> worker=new VLCWorker();
>> worker->moveToThread(&vlcWorkerThread);
>> worker->initVLC(videoWin);
>> vlcWorkerThread.start();
>>
>> The initVLC function, among other things, creates a timer and connects it to a slot in the worker:
>>
>> rtspStartTimer=new QTimer;
>> rtspStartTimer->setSingleShot(true);
>> connect(rtspStartTimer, &QTimer::timeout,
>> this, &VLCWorker::rtspStarting);
>>
>>
>> Later, at some point during program execution, I try to start this timer from a function in the worker (which itself was triggered by a signal from the main thread):
>>
>> qDebug()<<"*****Trying to start timer which lives in thread"<<(long)rtspStartTimer->thread()<<" from thread "<<(long)this->thread();
>> rtspStartTimer->start(250);
>> qDebug()<<"*****Timer attempt complete";
>>
>> Unfortunately, this doesn’t work, giving me the following output:
>>
>> Debug: 2021-07-18T19:26:12.009 - *****Trying to start timer which lives in thread 140235980165688 from thread 140235980165688 (../DoorBellCamC/vlccontroller.cpp:121, void VLCWorker::rtspStarting())
>> Warning: 2021-07-18T19:26:12.009 - QObject::startTimer: Timers cannot be started from another thread (:0, )
>> Debug: 2021-07-18T19:26:12.009 - *****Timer attempt complete (../DoorBellCamC/vlccontroller.cpp:123, void VLCWorker::rtspStarting())
>>
>> So even though the output of the debug confirms that the timer lives in the same thread I am trying to call start from, I still get the error about “timers cannot be started from another thread”. What am I doing wrong?
>> ---
>> Israel Brewster
>> Software Engineer
>> Alaska Volcano Observatory
>> Geophysical Institute - UAF
>> 2156 Koyukuk Drive
>> Fairbanks AK 99775-7320
>> Work: 907-474-5172
>> cell: 907-328-9145
>>
>> _______________________________________________
>> Interest mailing list
>> Interest at qt-project.org <mailto:Interest at qt-project.org>
>> https://lists.qt-project.org/listinfo/interest
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.qt-project.org/pipermail/interest/attachments/20210719/e0985031/attachment.html>
More information about the Interest
mailing list