ImageLoader detach

This commit is contained in:
hemirt 2017-10-01 00:48:24 +02:00
parent f89761a9e3
commit c22fcd0c5e
2 changed files with 5 additions and 14 deletions

View file

@ -18,7 +18,7 @@ namespace messages {
ImageLoader::ImageLoader()
{
this->worker = std::thread(&ImageLoader::run, this);
std::thread(&ImageLoader::run, this).detach();
}
ImageLoader::~ImageLoader()
@ -29,14 +29,10 @@ ImageLoader::~ImageLoader()
this->ready = true;
}
this->cv.notify_all();
qDebug() << "notified waiting";
this->worker.join();
qDebug() << "destruct";
}
void ImageLoader::run()
{
this->eventLoop.reset(new QEventLoop());
while (true) {
std::unique_lock<std::mutex> lk(this->workerM);
if (!this->ready) {
@ -52,17 +48,14 @@ void ImageLoader::run()
this->ready = false;
lk.unlock();
for (auto &lli : current) {
if (this->exit)
return;
QNetworkRequest request;
request.setUrl(QUrl(lli->url));
QNetworkAccessManager NaM;
QNetworkReply *reply = NaM.get(request);
QObject::connect(reply, &QNetworkReply::finished, this->eventLoop.get(),
&QEventLoop::quit);
qDebug() << "eve1";
this->eventLoop->exec(); // Wait until response is read.
qDebug() << "eve2";
QEventLoop eventLoop;
QObject::connect(reply, &QNetworkReply::finished, &eventLoop, &QEventLoop::quit);
// Wait until response is read.
eventLoop.exec();
qDebug() << "Received emote " << lli->url;
QByteArray array = reply->readAll();
QBuffer buffer(&array);

View file

@ -22,13 +22,11 @@ public:
private:
void run();
std::thread worker;
std::mutex workerM;
std::vector<chatterino::messages::LazyLoadedImage *> toProcess;
bool ready = false;
std::condition_variable cv;
bool exit = false;
std::unique_ptr<QEventLoop> eventLoop;
};
} // namespace messages