CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jul 2003
    Posts
    79

    Pass Document data to thread

    My application contains RS232 communication. The user should be able to pick which Com port to use. The seriall interface is controlled by a separate thread. When the user pick a Com port from the list an int is stored in the Document corresponding to a Com port. This int has to be passed to the seriall com thread. I have managed to get a pointer to the Document but I get an assertion about access violation when passing the int value.

    here is the assertion,
    _AFX_INLINE int CString::Compare(LPCTSTR lpsz) const
    { ASSERT(AfxIsValidString(lpsz)); return _tcscmp(m_pchData, lpsz); }

    here is the code used in the seriall com thread,

    CWinApp* theApp = AfxGetApp();
    appPos = theApp->GetFirstDocTemplatePosition();
    CDocTemplate* docTemplate = theApp->
    GetNextDocTemplate(appPos);
    docPos = docTemplate->GetFirstDocPosition();
    CGsdoc_b1Doc* pDoc = (CGsdoc_b1Doc*)docTemplat
    -> GetNextDoc(docPos);
    comPort = pDoc->GetComPort(); //Access violatio!!!
    comStr.Format("%s%d", "Com", comPort);

  2. #2
    Join Date
    Feb 2002
    Posts
    5,757
    There are several solutions. One solution is a pointer to a constant document member variable. Another solution is a global variable.

    Kuphryn

  3. #3
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    You can not pass MFC objects into a thread. Instead of passing
    the pointer to the app, pass the number in dircetly.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  4. #4
    Join Date
    Jul 2003
    Posts
    79
    How do I pass the number in directely? With a global variable? I have never used that before.

  5. #5
    Join Date
    Nov 2002
    Location
    Los Angeles, California
    Posts
    3,863
    This variable does not change during the lifetime of the thread, right?
    If so, then you do not need a global variable.

    One solution is to create a struct

    struct COM_PORT_THREAD_PARAMETERS
    {
    unsigned int ComPort_;
    //anything you want to pass in goes in here. NO MFC
    }

    then when it comes time to create the thread

    COM_PORT_THREAD_PARAMETERS thread_param = new COM_PORT_THREAD_PARAMETERS;
    and pass this pointer into the thread function.
    It becomes the threads responsibility to delete the pointer
    to free ther memory allocated.
    Wakeup in the morning and kick the day in the teeth!! Or something like that.

    "i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."

  6. #6
    Join Date
    Jul 2003
    Posts
    79
    It seems to work when using constant variables. Thanks guys.

  7. #7
    Join Date
    Jul 2003
    Posts
    79
    The thread is created in the InitInstance function. Since the com port value used in the thread comes from the Document the thread should not be started before all the Serialisation of the Document. So I create the thread in the beginning of InitInstance in suspended mode. Then the thread is started with ResumeThread at the end of InitInstance. But when debugging inside the Thread function a UM_FINISH message is recieved meaning that the thread should be terminated.
    I don't understand why.

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