我创建了一个用户界面线程MyThread,在MyThread类中添加了一个自定义的消息响应函数,如下:
void MyThread::MyMessageHandler(WPARAM, LPARAM)
{
CFrameWnd *l_FrameWnd;
CView *l_View;
l_FrameWnd = (CFrameWnd *)AfxGetApp()->m_pMainWnd;
l_View = l_FrameWnd ->GetActiveView();
l_View->SetTimer(1,100,NULL);//设置定时器
}
然后我在视图类中添加了菜单响应函数,如下:
void CMyThreadProjectView::OnThreadCreate() 创建用户界面线程
{
// TODO: Add your command handler code here
MyThread* pThread;
pThread = new MyThread();
pThread->CreateThread();
pThread->PostThreadMessage(WM_MYTHREADMESSAGE,NULL,NULL);
}
void CMyThreadProjectView::OnThreadSuspend() //结束用户界面线程
{
// TODO: Add your command handler code here pThread->PostThreadMessage(WM_QUIT,NULL,NULL);}
另外还在视图类中添加OnTimer()函数,用来实现对线程类中SetTimer()函数的响应,如下:
void CMyThreadProjectView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CFrameWnd *l_FrameWnd;
CView *l_View;
l_FrameWnd = (CFrameWnd *)AfxGetApp()->m_pMainWnd;
l_View = l_FrameWnd ->GetActiveView();
CClientDC dc(l_View);
dc.TextOut(x,y,"Hello");
x=x+30;
y = y+20; CView::OnTimer(nIDEvent);
}程序运行的结果是创建用户界面线程正常,在视图类中可描绘出"Hello"的字样,但在结束结束线程函数中添加pThread->PostThreadMessage(WM_QUIT,NULL,NULL);后去出现了应用程序遇到问题需要关闭的错误,我不知道这是什么原因引起的,还请大家能帮我诊断一下,谢谢!
不知道我有没有说清楚,如说的不清楚的话,还请包涵!!!!!
void MyThread::MyMessageHandler(WPARAM, LPARAM)
{
CFrameWnd *l_FrameWnd;
CView *l_View;
l_FrameWnd = (CFrameWnd *)AfxGetApp()->m_pMainWnd;
l_View = l_FrameWnd ->GetActiveView();
l_View->SetTimer(1,100,NULL);//设置定时器
}
然后我在视图类中添加了菜单响应函数,如下:
void CMyThreadProjectView::OnThreadCreate() 创建用户界面线程
{
// TODO: Add your command handler code here
MyThread* pThread;
pThread = new MyThread();
pThread->CreateThread();
pThread->PostThreadMessage(WM_MYTHREADMESSAGE,NULL,NULL);
}
void CMyThreadProjectView::OnThreadSuspend() //结束用户界面线程
{
// TODO: Add your command handler code here pThread->PostThreadMessage(WM_QUIT,NULL,NULL);}
另外还在视图类中添加OnTimer()函数,用来实现对线程类中SetTimer()函数的响应,如下:
void CMyThreadProjectView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
CFrameWnd *l_FrameWnd;
CView *l_View;
l_FrameWnd = (CFrameWnd *)AfxGetApp()->m_pMainWnd;
l_View = l_FrameWnd ->GetActiveView();
CClientDC dc(l_View);
dc.TextOut(x,y,"Hello");
x=x+30;
y = y+20; CView::OnTimer(nIDEvent);
}程序运行的结果是创建用户界面线程正常,在视图类中可描绘出"Hello"的字样,但在结束结束线程函数中添加pThread->PostThreadMessage(WM_QUIT,NULL,NULL);后去出现了应用程序遇到问题需要关闭的错误,我不知道这是什么原因引起的,还请大家能帮我诊断一下,谢谢!
不知道我有没有说清楚,如说的不清楚的话,还请包涵!!!!!
Do not post the WM_QUIT message using the PostMessage function; use PostQuitMessage
你应该在线程的消息循环内使用PostQuitMessage,你可以通过向线程发送自定义消息,在自定义消息的响应代码内调用PostQuitMessage。