Re: [Interest] Difficulty running Timer from QThread

2021-07-20 Thread Israel Brewster
For what it’s worth, it doesn’t appear that the thread in which the QTimer 
object lived was ever the problem. Even when I first asked the question, 
calling rtspStartTimer->thread() returned the correct thread (or at least the 
same thread as calling this->thread() from a parent class function).

While I have since implemented the suggestions of running the init function in 
response to the QThread started() signal, as well as adding a parent for the 
QTimer, the actual fix was when I discovered that the function that called 
start was *not* running in the thread I thought it was (i.e. some thread other 
than vlcWorkerThread).

From what I can tell, that function being called from the unknown thread was 
the complete cause of the issue.

Thanks all for the help!
---
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 20, 2021, at 10:40 AM, Björn Schäpers  wrote:
> 
> Am 20.07.2021 um 06:19 schrieb Thiago Macieira:
>> On Monday, 19 July 2021 11:51:30 PDT Björn Schäpers wrote:
>>> Yes, and the Timer was created inside the VLCWorker::initVLC, and said
>>> worker was moved, but before initVLC, but initVLC was called in the moved
>>> from thread.
>> But the QTimer had no parent. From the OP:
>> 
>> rtspStartTimer=new QTimer;
>> rtspStartTimer->setSingleShot(true);
>> connect(rtspStartTimer, ::timeout,
>> this, ::rtspStarting);
>> 
>> That means this QTimer wasn't moved and therefore was firing on the thread
>> that created the object. Whichever that was.
>> 
> Last response. Yes that's why I advised to parent it, and THEN move to the 
> other thread. The Timer would have been moved with its parent.
> 
> ___
> Interest mailing list
> Interest@qt-project.org
> https://lists.qt-project.org/listinfo/interest

___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] Difficulty running Timer from QThread

2021-07-20 Thread Björn Schäpers

Am 20.07.2021 um 06:19 schrieb Thiago Macieira:

On Monday, 19 July 2021 11:51:30 PDT Björn Schäpers wrote:

Yes, and the Timer was created inside the VLCWorker::initVLC, and said
worker was moved, but before initVLC, but initVLC was called in the moved
from thread.

But the QTimer had no parent. From the OP:

rtspStartTimer=new QTimer;
rtspStartTimer->setSingleShot(true);
connect(rtspStartTimer, ::timeout,
 this, ::rtspStarting);

That means this QTimer wasn't moved and therefore was firing on the thread
that created the object. Whichever that was.

Last response. Yes that's why I advised to parent it, and THEN move to the other 
thread. The Timer would have been moved with its parent.


___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] Difficulty running Timer from QThread

2021-07-19 Thread Thiago Macieira
On Monday, 19 July 2021 11:51:30 PDT Björn Schäpers wrote:
> Yes, and the Timer was created inside the VLCWorker::initVLC, and said
> worker was moved, but before initVLC, but initVLC was called in the moved
> from thread.

But the QTimer had no parent. From the OP:

rtspStartTimer=new QTimer;
rtspStartTimer->setSingleShot(true);
connect(rtspStartTimer, ::timeout,
this, ::rtspStarting);

That means this QTimer wasn't moved and therefore was firing on the thread 
that created the object. Whichever that was.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering



___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] Difficulty running Timer from QThread

2021-07-19 Thread Björn Schäpers

Am 19.07.2021 um 17:57 schrieb Thiago Macieira:

On Sunday, 18 July 2021 23:08:13 PDT Björn Schäpers wrote:

you should parent your QTimer, and moveToThread afterwards.

That shouldn't be "AND". That should be an exclusive OR: you either parent
your QObject or you moveToThread, not both. You cannot move a QObject that has
a parent to another thread. You can only move the entire hierarchy.

Yes, and the Timer was created inside the VLCWorker::initVLC, and said worker 
was moved, but before initVLC, but initVLC was called in the moved from thread.


___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] Difficulty running Timer from QThread

2021-07-19 Thread Thiago Macieira
On Sunday, 18 July 2021 23:08:13 PDT Björn Schäpers wrote:
> you should parent your QTimer, and moveToThread afterwards.

That shouldn't be "AND". That should be an exclusive OR: you either parent 
your QObject or you moveToThread, not both. You cannot move a QObject that has 
a parent to another thread. You can only move the entire hierarchy.

-- 
Thiago Macieira - thiago.macieira (AT) intel.com
  Software Architect - Intel DPG Cloud Engineering



___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] Difficulty running Timer from QThread

2021-07-19 Thread Israel Brewster
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  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 > > 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();
>> 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, ::timeout,
>>  this, ::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@qt-project.org 
>> https://lists.qt-project.org/listinfo/interest
> 

___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] Difficulty running Timer from QThread

2021-07-19 Thread Björn Schäpers

Hi,

you should parent your QTimer, and moveToThread afterwards. Then the Timer will 
also be moved to the thread, otherwise it remains whereever you created the worker.


Regards,
Björn.

Am 19.07.2021 um 05:29 schrieb Israel Brewster:
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=newVLCWorker();
worker->moveToThread();
worker->initVLC(videoWin);
vlcWorkerThread.start();

The initVLC function, among other things, creates a timer and connects it to a 
slot in the worker:


rtspStartTimer=newQTimer;
rtspStartTimer->setSingleShot(true);
connect(rtspStartTimer,::timeout,
this,::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()<<"*Tryingtostarttimerwhichlivesinthread"<<(long)rtspStartTimer->thread()<<"fromthread"<<(long)this->thread();
rtspStartTimer->start(250);
qDebug()<<"*Timerattemptcomplete";

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@qt-project.org
https://lists.qt-project.org/listinfo/interest



___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


Re: [Interest] Difficulty running Timer from QThread

2021-07-18 Thread Nuno Santos
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  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();
> 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, ::timeout,
>   this, ::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@qt-project.org
> https://lists.qt-project.org/listinfo/interest

___
Interest mailing list
Interest@qt-project.org
https://lists.qt-project.org/listinfo/interest


[Interest] Difficulty running Timer from QThread

2021-07-18 Thread Israel Brewster
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();
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, ::timeout,
this, ::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@qt-project.org
https://lists.qt-project.org/listinfo/interest