-
January 3rd, 2004, 01:05 PM
#1
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);
-
January 3rd, 2004, 01:08 PM
#2
There are several solutions. One solution is a pointer to a constant document member variable. Another solution is a global variable.
Kuphryn
-
January 3rd, 2004, 02:11 PM
#3
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."
-
January 3rd, 2004, 03:27 PM
#4
How do I pass the number in directely? With a global variable? I have never used that before.
-
January 3rd, 2004, 03:38 PM
#5
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."
-
January 3rd, 2004, 03:39 PM
#6
It seems to work when using constant variables. Thanks guys.
-
January 4th, 2004, 07:52 AM
#7
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|