|
-
August 20th, 1999, 04:05 AM
#1
New to threads and messages
I am trying to write a program which constantly polls the serial port waiting for an input (why I'm bothering I have no idea - I'm sure millions of other people have already done it successfully!) but anyway, I have created a new thread (using AfxBeginThread) to constantly check, and send a message to the main thread when it finds anything in the buffer:
UINT ThreadProc(LPVOID pParam)//global func called by the thread
{
CSerialDlg* pParent=(CSerialDlg*)pParam;
pParent->ReadSerial();
return 0;
}
void CSerialDlg::ReadSerial() //member function called by the global function
{
CString Output = "";
unsigned char OutputBuffer[1024];
int BytesRead;
CString Text;
while(1)
{
while((BytesRead = ReadComms(OutputBuffer, 1024)) > 0)
{
OutputBuffer[BytesRead] = '\0';
Output = OutputBuffer;
m_pOutputString = &Output;
this->PostMessage(WM_SERIAL, 0, 0); //it crashes somewhere around here
}
::Sleep(0);
}
}
The problem I have is that in release mode the program crashes after the second time the message is posted. This happens under NT and 95. In 95 I get an invalid page fault and in NT it just says "the memory cannot be read".
The message is declared as follows:
#define WM_SERIAL WM_USER+100
BEGIN_MESSAGE_MAP(CSerialDlg, CDialog)
//{{AFX_MSG_MAP(CSerialDlg)
...
ON_THREAD_MESSAGE(WM_SERIAL, OnSerialMessage)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
and in the .h file:
// Generated message map functions
//{{AFX_MSG(CSerialDlg)
...
afx_msg void OnSerialMessage();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
the message handler function is as follows:
void CSerialDlg::OnSerialMessage()
{
m_Output += *m_pOutputString;
m_Output.Replace("\r\n", "\n");
m_Output.Replace("\n", "\r\n");
UpdateData(FALSE);
}
I hope I have given enough information for someone to be able to help with this problem. This is my first attempt at working with threads or messages, so I really don't have much idea what I'm doing!
Thanks,
Jess
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
|