// TODO: Add your specialized code here and/or call the base class pDoc = GetDocument(); pDoc->SetTitle("AK47"); _beginthreadex(0,0,threadfunc,this,0,0); // AfxBeginThread(threadfunc, this);} 这段代码通过编译但执行报错! 我用Callstack查看了一下,发现是错在线程中的SetTitle()函数。我试着使用了一下GetTitle()却没问题!我怀疑是在线程之间传递了PDoc指针造成的,但却没有明显的证据。。
// TODO: Add your specialized code here and/or call the base class pDoc = GetDocument(); pDoc->SetTitle("AK47");// _beginthreadex(0,0,threadfunc,this,0,0); AfxBeginThread(threadfunc, this);}
class CA
{
public:
static WINAPI UINT threadfunc(LPVOID p);
int x;
...
}
...
CA::***()
{
_beginthreadex(0,0,threadfunc,this,0,0);
}
CA::threadfunc(LPVOID p)
{
CA* pa=(CA*)p;
pa->x.....
}
{
CObject* pObj=(Object*)arg;
pObj->MemberThreadProc();
}
//ThreadView.h#include "process.h"
class CThreadView : public CView
{
........
public:
CThreadDoc* pDoc;
static UINT WINAPI threadfunc(LPVOID pParam);
........
}//ThreadView.cpp
UINT CThreadView::threadfunc(LPVOID pParam)
{
CThreadView* pThreadView = (CThreadView*) pParam;
// CString strttl = pThreadView->pDoc->GetTitle();
// TRACE("%s\n",strttl);
pThreadView->pDoc->SetTitle("M4A1"); return 0;
}void CThreadView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
pDoc = GetDocument(); pDoc->SetTitle("AK47"); _beginthreadex(0,0,threadfunc,this,0,0);
// AfxBeginThread(threadfunc, this);}
这段代码通过编译但执行报错!
我用Callstack查看了一下,发现是错在线程中的SetTitle()函数。我试着使用了一下GetTitle()却没问题!我怀疑是在线程之间传递了PDoc指针造成的,但却没有明显的证据。。
用AfxBeginThread因为MFC函数在多线程里会有一些问题如果不用AfxBeginThread
试着改为:
//ThreadView.h
class CThreadView : public CView
{
........
public:
CThreadDoc* pDoc;
static UINT threadfunc(LPVOID pParam);
........
}//ThreadView.cpp
UINT CThreadView::threadfunc(LPVOID pParam)
{
CThreadView* pThreadView = (CThreadView*) pParam;
// CString strttl = pThreadView->pDoc->GetTitle();
// TRACE("%s\n",strttl);
pThreadView->pDoc->SetTitle("M4A1"); return 0;
}void CThreadView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
pDoc = GetDocument(); pDoc->SetTitle("AK47");// _beginthreadex(0,0,threadfunc,this,0,0);
AfxBeginThread(threadfunc, this);}
AfxBeginThread可能也会有问题,因为这个线程没有文档视图机制。如果你只想改标题的话,试试直接取主窗口用SetWindowText。
还有一种方法是你用postthreadmesssage给主线程发个消息,让主线程去调用doc的settile