主题:VC2008中遇到的问题
在用2008建立一MFC(exe),SDI,添加了线程菜单,在下面添加了工作者线程,ID_WORKTHREAD,添加了ONWORKTHREAD()函数到Cview中,在Cview.cpp中填入如下代码:
# include "afxmt.h"
CString MyString="I am using my frist thread";
编写Cview::OnWORKTHREAD()
{
// TODO: 在此添加命令处理程序代码
CDC *pDC=GetDC();
pDC->TextOutW(30,40,(const CString &)"String before change:");
//编译错误,我强制将“ ”转换成const CString &)",
pDC->TextOutW(50,80,MyString);
//CString pString=(const CString &)"I am using my first workthread now";
// pString=&MyString;
AfxBeginThread(MyWorkThreadFunction,(LPVOID)&MyString);//(LPVOID)pString
pDC->TextOutW(30,120, (const CString &)"String after change:");
pDC->TextOutW(50,160,MyString);
ReleaseDC(pDC);
}
继续添加函数
UINT MyWorkThreadFunction(LPVOID pParam)//LPVOID pParam
{
CWnd *pMainWnd=AfxGetMainWnd();
if(pParam==NULL)
{
LPCWSTR p=(LPCWSTR)"Param error";
::MessageBox(pMainWnd->m_hWnd,p,p,MB_OK);
AfxEndThread(2);
}
CString *pStr=(CString *)pParam;
pStr->MakeReverse();
LPCWSTR p2=(LPCWSTR)"Thread run";
::MessageBox(pMainWnd->m_hWnd,p2,p2,MB_OK);
return 0;
}
运行程序,编译通过,运行时出现错误;如图
谁能帮我改改
# include "afxmt.h"
CString MyString="I am using my frist thread";
编写Cview::OnWORKTHREAD()
{
// TODO: 在此添加命令处理程序代码
CDC *pDC=GetDC();
pDC->TextOutW(30,40,(const CString &)"String before change:");
//编译错误,我强制将“ ”转换成const CString &)",
pDC->TextOutW(50,80,MyString);
//CString pString=(const CString &)"I am using my first workthread now";
// pString=&MyString;
AfxBeginThread(MyWorkThreadFunction,(LPVOID)&MyString);//(LPVOID)pString
pDC->TextOutW(30,120, (const CString &)"String after change:");
pDC->TextOutW(50,160,MyString);
ReleaseDC(pDC);
}
继续添加函数
UINT MyWorkThreadFunction(LPVOID pParam)//LPVOID pParam
{
CWnd *pMainWnd=AfxGetMainWnd();
if(pParam==NULL)
{
LPCWSTR p=(LPCWSTR)"Param error";
::MessageBox(pMainWnd->m_hWnd,p,p,MB_OK);
AfxEndThread(2);
}
CString *pStr=(CString *)pParam;
pStr->MakeReverse();
LPCWSTR p2=(LPCWSTR)"Thread run";
::MessageBox(pMainWnd->m_hWnd,p2,p2,MB_OK);
return 0;
}
运行程序,编译通过,运行时出现错误;如图
谁能帮我改改