Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Native Development

Reply
Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later
Accepted Solution

Working with threads

[ Edited ]

How can I create a class thread with header file?

 

/*
 * NetworkThread.cpp
 *
 *  Created on: 1 Jan 2014
 *      Author: krikor
 */

#include "NetworkThread.hpp"
#include <QObject>
#include <QDebug>

void NetworkThread::run()
{
     // qDebug() << "hello from worker thread " ;
    qDebug() << "checked";

}

 

 


#include <QObject>
#include <QThread>

 class NetworkThread : public QThread
 {
     Q_OBJECT
 private:
     void run();
 };

 

 

 

How can I call a function?

 

 NetworkThread thread;
     thread.start();

   thread.getData():
     thread.wait();

 

I just want to lauch  a thread like thois

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: Working with threads

void NetworkThread::run()
{
	sleep(10);
	 qDebug() << "hello from worker thread " ;
}

void NetworkThread::getData()
{

}

 

NetworkThread thread;
	 thread.start();

	 thread.wait();

 

 

I did this, but original thread waits 10 seconds???

 

Plus I cannot modify qml via thread ?

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: Working with threads

QmlDocument *qml = QmlDocument::create("asset:///Tabs.qml").parent(this);

	root = qml->createRootObject<AbstractPane>();

	if (!qml->hasErrors()) {
		qml->setContextProperty("app", this);
	}


	Application::instance()->setScene(root);

	 NetworkThread thread;
	 thread.start();

	 thread.wait();

 

my goal is after i load page, i call a new thread...to change data on page.

why should my page not load and wait for network thread?

Developer
Posts: 828
Registered: ‎10-16-2012
My Device: Red Z10
My Carrier: Rogers

Re: Working with threads

[ Edited ]

anything you setup in your constructor is going to run before the qml page finishes loading.  so instead of starting the thread in your constructer place that portion in its own function and execute that.

 

this way the thread won't load until after your qml is loaded, the other option would be to run the thread and wait until it decides to allow your qml file to load.

 

I think the threading is supposed to do this at once but while in the constructor I belive everything is forced to wait until the constructor is completley finished executing its code.  

 

(mostly speculation, c++ isn't my strong point just a series of observation's i've made over time)

 

 

ApplicationUI::ApplicationUI{
  //constructor

  //load qml

  runThread();
}

ApplicationUI::runThread()
{
   NetworkThread thread;
      thread.start();
      thread.wait();
}

 .hpp

 

Q_INVOKABLE void runThread();

 

 

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: Working with threads

 NetworkThread thread;
	 thread.start();

 

no what u said makes no difference.

When I do dthis, I get QThread: Destroyed while thread is still running

 

I need to start the thread...but original thread to move on..

i dont want my original thread to wait for the other thread to finish thread.wait();

Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: Working with threads

That is not how you are supposed to use QThread. There are several other posts here on the forums, how to use QThread correctly, and I think there was even one saying Subclassing QThread is NOT the correct way

 

I use QThread myself, and the best way I found was this:

 

	QThread* thread = new QThread;
	TestWorker* worker = new TestWorker(myData);
	worker->moveToThread(thread);
	connect(worker, SIGNAL(error(QString)), this, SLOT(errorString(QString)));
	connect(thread, SIGNAL(started()), worker, SLOT(process()));
	connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
	connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
	connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));
	thread->start();

 Create a worker and move that worker to a newly created thread. And connect the signals and slots so you know rwhen your thread has finished

-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: Working with threads

I am working on that, but i get error

 

/*
 * EventWorker.cpp
 *
 *  Created on: 2 Jan 2014
 *      Author: krikor
 */

#include "EventWorker.hpp"
#include <QObject>
#include <QString>
#include <bb/cascades/Application>
#include <bb/cascades/QmlDocument>
#include <bb/cascades/AbstractPane>
#include <QSettings>



using namespace bb::cascades;

EventWorker::EventWorker() {
	// TODO Auto-generated constructor stub

}

EventWorker::~EventWorker() {
	// TODO Auto-generated destructor stub
}


void EventWorker::doWork()const
{
	qDebug() << "thread start";
	qDebug() << "thread over";
	emit finishedThread();
}

 

 

/*
 * EventWorker.hpp
 *
 *  Created on: 2 Jan 2014
 *      Author: krikor
 */

#ifndef EVENTWORKER_HPP_
#define EVENTWORKER_HPP_

#include <QObject>
#include <QString>

class EventWorker: public QObject {
public:
	EventWorker();
	virtual ~EventWorker();
public slots:
	void doWork()const;
	Q_SIGNALS:
		void finishedThread();
};


#endif /* EVENTWORKER_HPP_ */

 

EventWorker* worker = new EventWorker();
	connect(worker, SIGNAL(finishedThread()),this,SLOT(setData()));
	QThread* thread = new QThread(this);
	connect(thread, SIGNAL(started()),worker, SLOT(doWork()));
	connect(thread, SIGNAL(finished()),worker, SLOT(deleteLater()));
	worker->moveToThread(thread);
	thread->start();

void ApplicationUI::setData()
{

}

 

I get error on emit finishedThread();  passing 'const EventWorker' as 'this' argument of 'void EventWorker::finishedThread()' discards qualifiers [-fpermissive]

Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: Working with threads

if i remove emit finishedThread()

I get this 2 errors when I rn my code

Object::connect: No such signal QObject::finishedThread() in ../src/applicationui.cpp:118
Object::connect: No such slot QObject::doWork() in ../src/applicationui.cpp:120
Developer
Posts: 508
Registered: ‎01-19-2011
My Device: My Trusty Red Plane
My Carrier: Outer Space

Re: Working with threads

There are some other errors on your code.

 

Try this in your header

 


babakar wrote:

I am working on that, but i get error

 

/*
 * EventWorker.hpp
 *
 *  Created on: 2 Jan 2014
 *      Author: krikor
 */

#ifndef EVENTWORKER_HPP_
#define EVENTWORKER_HPP_

#include <QObject>
#include <QString>

class EventWorker: public QObject {

Q_OBJECT

public: EventWorker(); virtual ~EventWorker(); public slots: void doWork(); //NO CONST
signals: void finishedThread(); }; #endif /* EVENTWORKER_HPP_ */

 




-----------------------------------------------------------------------
I'm a bird from outer space. But I'm not flappy o.o
Developer
Posts: 813
Registered: ‎05-29-2012
My Device: see later
My Carrier: see later

Re: Working with threads

/*
 * EventWorker.hpp
 *
 *  Created on: 2 Jan 2014
 *      Author: krikor
 */

#ifndef EVENTWORKER_HPP_
#define EVENTWORKER_HPP_

#include <QObject>
#include <QString>

class EventWorker: public QObject {

         Q_OBJECT


public:
	EventWorker();
	virtual ~EventWorker();
public slots:
	void doWork(); //NO CONST

signals:
	void finishedThread();
};

 

/*
 * EventWorker.cpp
 *
 *  Created on: 2 Jan 2014
 *      Author: krikor
 */

#include "EventWorker.hpp"
#include <QtCore/QObject>
#include <QString>
#include <bb/cascades/Application>




using namespace bb::cascades;

EventWorker::EventWorker() {
	// TODO Auto-generated constructor stub

}

EventWorker::~EventWorker() {
	// TODO Auto-generated destructor stub
}


void EventWorker::doWork()
{
	qDebug() << "thread start";
	qDebug() << "thread over";
	emit finishedThread();
}

 

i get errors undefined reference to `vtable for EventWorker'

 

undefined reference to `EventWorker::finishedThread()'