[Interest] Set a timer for a different thread
Thiago Macieira
thiago.macieira at intel.com
Thu Apr 25 03:59:11 CEST 2019
On Wednesday, 24 April 2019 14:11:09 PDT Tom Isaacson wrote:
> I've got some legacy code where a timer is firing on the wrong thread. The
> sequence is: 1. Thread instantiates MyClass.
> 2. MyClass constructor calls QTimer::singleShot(60 * 1000, this,
> &MyClass::OnTimer); 3. Move class to thread by calling
> MyClass.moveToThread();
> 4. OnTimer is called on the wrong thread.
>
> I assume because the constructor sets the timer before the class is moved to
> the thread, the timer is firing on the wrong thread. Obviously I can move
> the timer to a different function then call that after moveToThread() but
> is there a standard way of fixing this?
Don't use QTimer::singleShot, but instead use a QTimer object whose parent is
the object that you will move. That will move the timer too, by unregistering
it in the original thread and re-registering in the target thread once the
move is complete.
--
Thiago Macieira - thiago.macieira (AT) intel.com
Software Architect - Intel System Software Products
More information about the Interest
mailing list