How to Sleep in Plugin?

Questions related to plugins development
Post Reply
CCNewbieL6
Posts: 23
Joined: Wed Jan 27, 2021 11:11 am

How to Sleep in Plugin?

Post 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!
daniel
Site Admin
Posts: 7382
Joined: Wed Oct 13, 2010 7:34 am
Location: Grenoble, France
Contact:

Re: How to Sleep in Plugin?

Post 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)
Daniel, CloudCompare admin
CCNewbieL6
Posts: 23
Joined: Wed Jan 27, 2021 11:11 am

Re: How to Sleep in Plugin?

Post by CCNewbieL6 »

Thank you, daniel!
CCNewbieL6
Posts: 23
Joined: Wed Jan 27, 2021 11:11 am

Re: How to Sleep in Plugin?

Post 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);

Post Reply