Hi All,
I am working on BB10 native extension which is written in QT. I have started a thread to execute my network request.
void* SignalThread(void* parent) {
TemplateJS *pParent = static_cast<TemplateJS *>(parent);
int argc = 0;
char **argv = NULL;
webworks::TemplateNDK *m_signalHandler = new webworks::TemplateNDK(pParent);
m_signalHandler->doNetworkRequest(); //sending network request
delete m_signalHandler;
return NULL;
}
bool TemplateJS::StartThread(){
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
pthread_t m_thread;
pthread_create(&m_thread, &thread_attr, SignalThread, static_cast<void *>(this));
pthread_attr_destroy(&thread_attr);
if (!m_thread) {
return true;
} else {
return false;
}
}
void* SignalThread(void* parent) {
TemplateJS *pParent = static_cast<TemplateJS *>(parent);
int argc = 0;
char **argv = NULL;
QCoreApplication QCoreApplication(argc, argv);
webworks::TemplateNDK *m_signalHandler = new webworks::TemplateNDK(pParent);
m_signalHandler->doNetworkRequest(); //sending network request
QCoreApplication::exec();
delete m_signalHandler;
return NULL;
}
bool TemplateJS::StartThread(){
pthread_attr_t thread_attr;
pthread_attr_init(&thread_attr);
pthread_attr_setdetachstate(&thread_attr, PTHREAD_CREATE_DETACHED);
pthread_t m_thread;
pthread_create(&m_thread, &thread_attr, SignalThread, static_cast<void *>(this));
pthread_attr_destroy(&thread_attr);
if (!m_thread) {
return true;
} else {
return false;
}
}
To copy to clipboard, switch view to plain text mode
Here is my simple code to start request.
void TemplateNDK::doNetworkRequest()
{
_networkAccessManager = new QNetworkAccessManager();
QObject::connect(_networkAccessManager,
SIGNAL(finished
(QNetworkReply
*)),
this,
SLOT(onRequestFinished(QNetworkReply*)));
QNetworkRequest request = QNetworkRequest();
request.
setUrl(QUrl(inputUrl
));
QNetworkReply* response = _networkAccessManager->get(request);
}
void TemplateNDK::doNetworkRequest()
{
_networkAccessManager = new QNetworkAccessManager();
QObject::connect(_networkAccessManager, SIGNAL(finished(QNetworkReply*)), this,
SLOT(onRequestFinished(QNetworkReply*)));
QNetworkRequest request = QNetworkRequest();
QString inputUrl = QString::fromUtf8(url.c_str());
request.setUrl(QUrl(inputUrl));
QNetworkReply* response = _networkAccessManager->get(request);
}
To copy to clipboard, switch view to plain text mode
When i call startThread() function first time slot returns proper response. But when I call same startThread() function second time it stuck and does not execute slot. QObject::connect is returning true both times.
If using QTimer to abort request after interval then second time onRequestFinished slot is getting executed and returns value of reply->error() as 5 that is "operation canceled".
Am i doing something wrong with QCoreApplication::exec().
Please help.
Thanks.
Bookmarks