Page 1 of 1

How to Sleep in Plugin?

Posted: Fri Mar 05, 2021 6:12 pm
by CCNewbieL6
Hello!

When I try to Sleep(10000) in my plugin, a plugin with GUI, the plugin crushed.

The Sleep() is to wait a thread, which is doing tasks.

Code: Select all

......
#include <thread>
#include <windows.h>
......
void CCLivoxLOAMDlg::on_StartButton_clicked() {
......
	//std::this_thread::sleep_for(std::chrono::seconds(100));   // crush here
	Sleep(10000);   // here too
......
}

Whether I need to create a new thread to run the Sleep() code?

Thanks!

Re: How to Sleep in Plugin?

Posted: Fri Mar 05, 2021 10:27 pm
by daniel
With Qt you wouldn't sleep in the main thread (as it blocks everything). The best would be to have a QTimer (single shot) that would trigger a 'slot' function after 10000 ms for instance (see QTimer::timeout - https://doc.qt.io/qt-5/qtimer.html#timeout)

Re: How to Sleep in Plugin?

Posted: Sat Mar 06, 2021 6:31 pm
by CCNewbieL6
Thank you, daniel!

Re: How to Sleep in Plugin?

Posted: Sun Mar 07, 2021 11:54 am
by CCNewbieL6
I solved this problem by creating a thread.
To create a thread to do something like sleep().

This way:

Code: Select all

#include <thread>
#include <windows.h>

void Scanner(int a){
	......
	Sleep(100);
}
......

std::thread *lidarThread;

//To run Scanner(1) in a thread;
lidarThread = new std::thread(Scanner, 1);