Re: MFC Worker Threads ??
I hope the follwoing sample codes will help you.
UINT MyThreadProc( LPVOID pParam )
{
CMyObject* pObject = (CMyObject*)pParam;
if (pObject == NULL ||
!pObject->IsKindOf(RUNTIME_CLASS(CMyObject)))
return 1;
// do something with 'pObject'
return 0; // thread completed successfully
}
//inside a different function in the program
.
.
.
pNewObject = new CMyObject;
AfxBeginThread(MyThreadProc, pNewObject);
Re: MFC Worker Threads ??
Is there a way that I can make the worker thread a member of the controls class?
Thanks,
sfought
Re: MFC Worker Threads ??
You can make the threadproc a member of your class if you make it static.
Mats Bejedahl
Re: MFC Worker Threads ??
Use a User Interface thread (CWinThread) instead, then override the virtual
function 'Run' to turn it into a Worker Thread.
This way the thread is a proper class without any static members.
eg.
class CMyThread : public CWinThread
....
In the Run function you could have a forever loop which waits on events,
when the Event is set some work is performed.
When the Run function exits the Thread terminates automatically.
The thread is started normally with
m_pThread = (CMyThread*) AfxBeginThread(RUNTIME_CLASS(CMyThread));
Jeff Nuttall
Re: MFC Worker Threads ??
for example:
If you have a worker thread in CView.
//in the header
public:
void StartThread();
// Generated message map functions
protected:
//{{AFX_MSG(Yoiurclassname)
afx_msg void OnNewThread();
DECLARE_MESSAGE_MAP()
};
// in the cpp file
BEGIN_MESSAGE_MAP(yourclassname, CView)
//{{AFX_MSG_MAP(yourclassname)
ON_COMMAND(ID_NEW_*****, OnNewThread)
END_MESSAGE_MAP()
void Cyourclassname::StartThread()
{
use your worker class here.
}
void Cyourclassname::OnNewThread()
{
StartThread();
}
PS: I hope this will help you. if you really need a real code, let me know.