void ThreadFunc()
{
CTime time;
CString strTime;
m_bRun=TRUE;
while(m_bRun)
{
time=CTime::GetCurrentTime();
strTime=time.Format("%H:%M:%S");
::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_TIME,strTime);//出错
Sleep(1000);
}
}
void CSingleThreadDlg::OnBnClickedStart()
{
hThread=CreateThread(NULL,0,(LPTHREAD_START_ROUTINE)ThreadFunc,
NULL,0,&ThreadID);
GetDlgItem(IDC_START)->EnableWindow(FALSE);
GetDlgItem(IDC_STOP)->EnableWindow(TRUE);
}
执行的时候::SetDlgItemText(AfxGetMainWnd()->m_hWnd,IDC_TIME,strTime);//出错,SingleThread.exe 中的 0x00413437 处有未经处理的异常: 0xC0000005: 读取位置 0x00000020 时发生访问冲突。
但是在vc6.0里运行正常。多线程编写时vs2010发生访问冲突。请大侠指点下。
------解决方案--------------------
非界面线程不能直接访问界面,需发送消息方式
------解决方案--------------------
可以用PostMessage,然后在消息处理的过程中去访问界面