The header file
public:
Afx_msg void OnBnClickedButton1 ();
CEdit m_edit1;
CEdit m_edit2;
Afx_msg void OnEnChangeEdit2 ();
Afx_msg void OnBnClickedStart ();
CButton m_button_say;
CWinThread * hThread0;
CWinThread * hThread1;
The SOCKET sock;
The static UINT _cdecl onListen0 (LPVOID lpvThreadParm);
The static UINT _cdecl onRecieve1 (LPVOID lpvThreadParm);
The thread function:
/////server monitoring thread/////
UINT CBLUETOOTHDlg: : onListen0 (LPVOID lpvThreadParm)
{
//CBLUETOOTHDlg m_blu;
Pthis CBLUETOOTHDlg *=(CBLUETOOTHDlg *) lpvThreadParm;
//pthis - & gt; OnBnClickedStart ();
SOCKADDR_BTH sa;
Int sa_len=sizeof (sa);
While (1)
{
Memset (& amp; Sa, 0, sa_len);
Pthis - & gt; The sock=accept (sockTCP, (LPSOCKADDR) & amp; Sa, & amp; Sa_len);
If (SOCKET_ERROR==pthis - & gt; The sock)
{
//AfxMessageBox (" sock listening socket failed!" );
}
The else//connection success
{
Pthis - & gt; M_edit1. SetSel (pthis - & gt; M_edit1. GetWindowTextLengthA (), pthis - & gt; M_edit1. GetWindowTextLengthA ());
Pthis - & gt; M_edit1. ReplaceSel (" client connection success!" );
Pthis - & gt; M_button_say. EnableWindow (TRUE);
Pthis - & gt; HThread1 - & gt; ResumeThread ();
Pthis - & gt; BThread1=TRUE;
Pthis - & gt; HThread1=AfxBeginThread (onRecieve1 lpvThreadParm, THREAD_PRIORITY_NORMAL, 0, 0, NULL);
}
Why a call pthis pointer m_edit1 error: 0 xc0000005: while reading position 0 x00000048 access conflict,
CodePudding user response:
Background threads are not allowed to directly access interface, it is better to through the way of sending a messageCodePudding user response:
Multiple Threads in the User Interface of http://msdn.microsoft.com/zh-cn/library/ms810439.aspxCodePudding user response:
PostMessage send a message to the main thread, and then the main thread to CEdit operating controlsCodePudding user response:
MFC class is not thread safe, don't directly in the thread function to access the UI thread of control information,CodePudding user response:
The child thread cannot directly manipulate the UI
Android is,
Bypass way is to send messages, android is to use handler, win32 using postmessage