|
-
February 27th, 2005, 12:20 AM
#1
HOWTO loop a derived CWinThread?
Hello everyone,
I have tried to get a derived CWinThread to loop and so far have not had any luck. Here is what I have:
BOOL InterfaceDlg::OnInitDialog()
{
StartProcessThread();
}
void CEllistar_InterfaceDlg::StartProcessThread()
{
pThread = (CReaderThread*) AfxBeginThread(RUNTIME_CLASS(CReaderThread), THREAD_PRIORITY_NORMAL,NULL,0,NULL);
if (pThread)
{
m_ThreadList.Add(pThread);
pThread->SetLogWindow(this);
}
}
Inside of CReaderThread I have:
BOOL CReaderThread::InitInstance()
{
while(flag != 0)
{
// my processing code here
}
int CReaderThread::ExitInstance()
{
// TODO: perform any per-thread cleanup here
return CWinThread::ExitInstance();
}
return FALSE;
}
Can anyone spot why this code does not loop? When I trace it it enters InitInstance, but does not run any of the code inside it!
-
February 27th, 2005, 06:40 AM
#2
Re: HOWTO loop a derived CWinThread?
-
February 27th, 2005, 06:44 AM
#3
Re: HOWTO loop a derived CWinThread?
 Originally Posted by gknight
Can anyone spot why this code does not loop? When I trace it it enters InitInstance, but does not run any of the code inside it! 
Well...pointing out the obvious...'flag' might be 0... 
What kind of type is it? Where does it get set?
-
February 28th, 2005, 04:36 PM
#4
Re: HOWTO loop a derived CWinThread?
At a more fundamental level, it's generally unwise to "loop forever" in a thread based on CWinThread. The reason is that CWinThread threads are UI threads, and "looping forever" will completely block the thread's message loop and defeat the ability to run a UI in the thread. For example, in the code you posted, it generally will not be possible to reach the ExitInstance code (but it's hard to tell since the braces in your pseudo-code do not line up).
If you really want to "loop forever", then use a pure worker thread.
Mike
-
February 28th, 2005, 09:30 PM
#5
Re: HOWTO loop a derived CWinThread?
Do you have an example of a pure worker thread? I havent found an example on this site.
-
February 28th, 2005, 10:07 PM
#6
Re: HOWTO loop a derived CWinThread?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|