class threadedScraper
: public QThread{
Q_OBJECT
int _id;
QMutex& _mtx;
void run() override
{
_filesystemwatcher->moveToThread(this);
_filesystemwatcher->addPath(_path);
// I tried different connection types. Qt::ConnectionType::DirectConnection does not work either
if (! connect(_filesystemwatcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(ondirectoryChanged(const QString&))))
{
qDebug() << "failure to connect to QFileSystemWatcher::directoryChanged signal";
return;
}
qDebug
() <<
QString("cycle %1: waiting for file creation in %2").
arg(_id
).
arg(_path
);
exec();
qDebug
() <<
QString("cycle %1: stopped waiting for file").
arg(_id
);
}
private slots:
void ondirectoryChanged (const QString& p)
{
qDebug
() <<
"cycle:" <<
QString::number(_id
) <<
"file:" <<p;
_mtx.unlock();
}
public:
threadedScraper
(int id, QMutex
& m,
QString p
) : _id
(id
), _mtx
(m
), _path
(p
), _filesystemwatcher
(nullptr
),
QThread() {
}
virtual ~threadedScraper()
{
if (_filesystemwatcher != nullptr)
delete _filesystemwatcher;
}
};
int main(int argc, char *argv[])
{
QString directoryToMonitor
="c:/temp/files";
QTimer::singleShot(2000,
&a,
[&](){ qDebug()<< "started scraping";
for (int i = 0; i < 2; ++i) {
qDebug
() <<
"starting cycle " <<
QString::number(i
);
threadedScraper worker (i, mtx, directoryToMonitor);
worker.start();
mtx.lock();
wc.wait(&mtx, 5000); // I need to wait until the file is found in the monitoring directory or 5 seconds elapsed
worker.exit(0); // stop the thread
worker.wait();
mtx.unlock();
}
qDebug()<< "finished scraping";
});
return a.exec();
}
class threadedScraper : public QThread
{
Q_OBJECT
int _id;
QMutex& _mtx;
QString _path;
QFileSystemWatcher* _filesystemwatcher;
void run() override
{
_filesystemwatcher = new QFileSystemWatcher();
_filesystemwatcher->moveToThread(this);
_filesystemwatcher->addPath(_path);
// I tried different connection types. Qt::ConnectionType::DirectConnection does not work either
if (! connect(_filesystemwatcher, SIGNAL(directoryChanged(const QString&)), this, SLOT(ondirectoryChanged(const QString&))))
{
qDebug() << "failure to connect to QFileSystemWatcher::directoryChanged signal";
return;
}
qDebug() << QString("cycle %1: waiting for file creation in %2").arg(_id).arg(_path);
exec();
qDebug() << QString("cycle %1: stopped waiting for file").arg(_id);
}
private slots:
void ondirectoryChanged (const QString& p)
{
qDebug() << "cycle:" << QString::number(_id) << "file:" <<p;
_mtx.unlock();
}
public:
threadedScraper(int id, QMutex& m, QString p) : _id(id), _mtx(m), _path(p), _filesystemwatcher(nullptr), QThread()
{
}
virtual ~threadedScraper()
{
if (_filesystemwatcher != nullptr)
delete _filesystemwatcher;
}
};
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
QMutex mtx;
QString directoryToMonitor="c:/temp/files";
QWaitCondition wc;
QTimer::singleShot(2000,&a, [&](){
qDebug()<< "started scraping";
for (int i = 0; i < 2; ++i) {
qDebug() << "starting cycle " << QString::number(i);
threadedScraper worker (i, mtx, directoryToMonitor);
worker.start();
mtx.lock();
wc.wait(&mtx, 5000); // I need to wait until the file is found in the monitoring directory or 5 seconds elapsed
worker.exit(0); // stop the thread
worker.wait();
mtx.unlock();
}
qDebug()<< "finished scraping";
});
return a.exec();
}
To copy to clipboard, switch view to plain text mode
Bookmarks