AfxBeginThread throws exception
The Document in my SDI application owns an object that takes care of some seriall communication the class is called CSerComm.
In Document .h file
CSerComm m_SerCommObj;
Since the worker thread should be created at start up a function in the Document is called in InitInstance which will create the thread. The Document function is called StartThread()
In InitInstance function
Code:
//Get Document pointer
POSITION pos = this->GetFirstDocTemplatePosition();
CGsdoc_b1Doc* pDoc = (CGsdoc_b1Doc*)this->GetNextDocTemplate(pos);
//Open the document
if(!((CDocTemplate*)pDocTemplate)->OpenDocumentFile(docFile))
{
pDoc->OnSaveDocument(docFile);
pDoc->OnOpenDocument(docFile);
}//create file if application run first time
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
// Enable drag/drop open
m_pMainWnd->DragAcceptFiles();
//Signal to Document to create the thread
pDoc->StartThread();
In StartThread a CSerComm function is called that the StartThread
Code:
m_SerCommObj.CreateThread(); //start communication
In CreateThread() function AfxBeginThread is called
m_pThread = AfxBeginThread(ThreadFunc, this);
But an exception is thrown when AfxBeginThread is called.
Why is that?
It works better if I let the Application class own the CSerrComm object or if the m_SerCommObj.CreateThread() is called in the OnNewDocument function in the Document. But this does'nt suite other requierments.
[Andreas]: Added code tags...