CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 16

Threaded View

  1. #1
    Join Date
    Jul 2003
    Posts
    79

    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...
    Last edited by Andreas Masur; March 6th, 2004 at 05:29 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured