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
oddboy
Posts: 136
Registered: ‎05-24-2011
My Carrier: rogers

determine when a thread is quit()

Hi gang,

 

I have a simple class that i push to a qthread using myClass->moveToThread(thread).

 

everythign works great.

 

there are times that I need to interrupt a thread.  to do so, i do: 

QThread * my_t = findChildren<QThread *>("myClassObjectName");
my_t->quit();

 This seems to work.  In my test harness, i spin up 3 threads that don't do anything, just call a singleShot timer and print some debug messages.

 

Using the above method, I can find the thread I'm interested in my objectName and quit() it.

 

However, I'm trying to figure out how i can tell if the object myClass runs through to completion, or if it was quit by calling quit.

 

the only thing I've been able to determine so far is that when I quit, the thread that quits doesn't emit the finished() signal which I call at the end of my processing... 

 

so, is there a way to tell when that the thread was quit() instead of running to completion and ending "naturally"?

 

thanks!

 

J

 

bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com
Please use plain text.
BlackBerry Development Advisor
mwoolley
Posts: 559
Registered: ‎06-25-2010
My Carrier: Vodafone

Re: determine when a thread is quit()

Hi

 

I may have misunderstood your full scenario, but on exiting your thread by whatever route, you still have your object so coudln't you simply set a bool to true on "natural" exit from the thread and test it? If true you exited naturally, if false you exited because quit was called on the thread.

 

Any help?

 

Regards

 

Martin

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Please use plain text.
Developer
oddboy
Posts: 136
Registered: ‎05-24-2011
My Carrier: rogers

Re: determine when a thread is quit()

Hi, thanks for your reply!

 

I don't quite follow this part "..you still have your object.."... how would i check the bool?

 

i could expose it as a property for the object, but not sure how to test it when i call quit... 

 

here's the code I'm using to find and quit() the thread:

 

void MyWorkManager::findThreads() {
	QThread * my_t = findChild<QThread *>(QString("myThread-12"));  //hard coded objectName for testing
	qDebug() << "MyWorkManager::findThreads() found : " << my_t;
	if (my_t != 0) {
	    my_t->quit();
	}
    }
}

 

would testing the bool be done here?  or in a signal/slot?

 

I have some QObject conntions set up when i create the object and push it into a thread.. the typical 

connect(thread, SIGNAL(started()), worker, SLOT(doStuff()));
connect(thread, SIGNAL(started()), this, SLOT(onThreadStarted()));
connect(thread, SIGNAL(destroyed()), this, SLOT(onThreadDestroyed()));
connect(worker, SIGNAL(finished()), this, SLOT(onThreadFinished()));
connect(worker, SIGNAL(finished()), thread, SLOT(quit()));
connect(worker, SIGNAL(finished()), worker, SLOT(deleteLater()));
connect(thread, SIGNAL(finished()), thread, SLOT(deleteLater()));

 i can see the thread start and the doStuff() get executed.

 

when a thread runs through to completion and ends "naturally", i see the finished() get fired, and onThreadFinished() gets called.

 

when i quit() the thread externally (code sample above), i see that onThreadDestroyed() gets called.  (note: onThreadDestroyed() also gets called when the thread exits naturally of course, but after onThreadFinished()).

 

 

 

(sorry, i am a bit of a noob still..)

 

thanks!

 

J

 

bron: a cron-like scheduler for BlackBerry 10
http://apps.oddelement.com
Please use plain text.
BlackBerry Development Advisor
mwoolley
Posts: 559
Registered: ‎06-25-2010
My Carrier: Vodafone

Re: determine when a thread is quit()

Hi and don't apologise. We're here to (try!) to help :-)

 

Anyway.... myClass is the object that contains the code you're executing in your thread. It also contains member variables that have values. When your thread stops executing you should still have access to myClass and its variables so you should be able to check the content of the bool I mentioned.

--------------------------------------------------------------------------------------------
Feel free to press the like button on the right side if you liked my attempts to help :-)
And please mark posts as solved if you think I found the solution or set you on its path. Thanks!
Follow me on Twitter: @mdwrim
Please use plain text.