Click to See Complete Forum and Search --> : ODD Problem, The Worker Thread RUNS ONCE !


Hadi Rezaie
March 12th, 2006, 06:11 PM
Hi, :(

I don't know what's problem with my Worker Thread ...
But it just runs once !

I simply create my thread with CreateThread and pass an integer member variable as a parameter and in ThreadFunc body i add +1 to the member variable ...
I made a break point to check how many times the thread runs ... and it runs just once ! :(

Here it's my code:

void CTestingDlg::OnBnClickedOk()
{
m_nNum = 0;
m_hT = CreateThread( NULL, 0, TFunc, &m_nNum, 0, &m_threadID2 );
}

DWORD WINAPI CTestingDlg::TFunc( LPVOID lpParameter )
{
int *n = (int *) lpParameter; // I add break point here ...
*n = *n + 1;
return 0;
}
Help plz ...

kirants
March 12th, 2006, 06:22 PM
What do you mean by runs once.
The thread runs as long as it's procedure doesn't return. In your case, the TFunc threadproc returns as soon as it dies *n = *n + 1. And that's the end of it.

If you want it to run again, you need to again trigger OnBnClickedOk()

gstercken
March 12th, 2006, 07:46 PM
If you want it to run again, you need to again trigger OnBnClickedOk()Either that - or, what's more common for worker threads, let them execute a loop.

Andreas Masur
March 13th, 2006, 02:48 AM
[ Redirected thread ]