How to restart a thread in mfc
Posted on 2007-11-27
I have an application that collects data from sensors on several com ports. Once all the data is collected I "do something" with it. Each sensor is interogated sequentially, based on its positon in a database. I have been tasked to collect data using multiple threads (1 for each com port that is active). My experience with threads is limited to a few worker threads here and there.
What I have done:
Created a class called "CComDataThread" that has the thread function
On start of program:
for(int i = 0; i < numberofComPortsInUse; i++)
create a new comdatathread object;
do some class init stuff
set the thread autodelete to false
create the thread suspended
add the thread object to an object array for use later on when I need to collect data
- a "poor mans" thread pool. (I don't know if this is the correct way to go about this yet...)
At some time later to collect data:
//for(int t = 0; t < threadpool.GetCount(); t++) - just test one thread now
for(int t = 0; t < 1; t++)
// get the thread object out of the array
CComDataThread* pThread = (CComDataThread*)ThreadPool->GetAt(t);
DWORD wait = WaitForSingleObject(pThread->m_hThread, INFINITE);
if(wait == WAIT_OBJECT_0)
break; // thread has exited go do something with the data
This actually works - the thread launches and data collection occurs. The problem is when I need to colect data again - the thread won't run again.
I have a lot to learn about this stuff and I'm sure I'll be posting a lot more questions re multithreading, but for now all I want to do is figure out how to get my thread(s) to run and rerun...
(I can attach real code if needed)