Doc / View:将Doc指针传递给线程函数时遇到问题。 [英] Doc/View : have problem in passing a Doc pointer to thread funcion.

查看:97
本文介绍了Doc / View:将Doc指针传递给线程函数时遇到问题。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将一个Doc指针传递给我的线程函数时遇到了问题。它的工作原理如下:

 CMegaDoc :: OnStartProcess()
{
AfxBeginThread(ThreadProcess,(LPVOID) this );
}
CMegaDoc :: OnOpenDocument(filename)
{
ReadFile(filename); // 我很确定这个函数没有问题。
}
ThreadProcess(LPVOID pParam)
{
CMegaDoc * pDoc = (CMegaDoc *)pParam;
pDoc-> OnOpenDocument(filename); // 调用此方法时可能会出现问题。
某些...
pDoc-> UpDateAllViews(NULL);
}





 CMegaView :: DrawData(CDC * pDC)
{
CMegaDoc pDoc = GetDocument();
int nCount = pDoc-> m_CenterCoord.GetSize();
if (nCount> = 2
{
for int i = 0 ; i< nCount ; i ++)
{
int x1 = pDoc-> m_CenterCoord.GetAt(i).x;
int y1 = pDoc-> m_CenterCoord.GetAt(i).y;
int x2 = pDoc-> m_CenterCoord.GetAt(i + 1).x;
int y2 = pDoc-> m_CenterCoord.GetAt(i + 1).x;
pDC-> MoveTo(x1,y1);
pDC-> LineTo(x2,y2);

}
}

}

CMegaView :: OnDraw(CDC * pDC)
{
DrawData (CDC * pDC);
}





这里的问题是,当运行Release版本时,它可以正常工作,而Debug版本遇到Debug断言失败.Debugger说存在访问冲突,但我不明白为什么以及如何解决它。

解决方案

参见多线程阅读:编程技巧 [ ^ ]。



您没有告诉我们您的 CMegaDoc 类。如果它来自 CDocument 或包含窗口句柄,并且从访问窗口处理的线程调用成员函数,则会有断言:



MFC不支持从未创建窗口的线程访问窗口。调试断言可能发生在 CWnd :: AssertValid()中。请参阅 wincore.cpp 中的源代码和有关多线程应用程序的注释。



如果您的应用程序发生这种情况,您必须更改设计

Hi, I''ve got a problem in passing a Doc pointer to my thread function.It worked like this:

CMegaDoc::OnStartProcess()
{
  AfxBeginThread(ThreadProcess,(LPVOID)this);
}
 CMegaDoc::OnOpenDocument(filename)
{ 
  ReadFile(filename);// I'm quite sure there is no problem in this function.
}
ThreadProcess(LPVOID pParam)
{
  CMegaDoc *pDoc = (CMegaDoc *)pParam;
  pDoc->OnOpenDocument(filename);//it's hihgly probable that something goes wrong when calling this method.
  do someting...
  pDoc->UpDateAllViews(NULL);
}



CMegaView::DrawData(CDC* pDC)
{
  CMegaDoc pDoc = GetDocument();
  int nCount = pDoc->m_CenterCoord.GetSize();
  if(nCount>=2)
 {
     for(int i=0;i<nCount;i++)
     {
        int x1 = pDoc->m_CenterCoord.GetAt(i).x;
        int y1 = pDoc->m_CenterCoord.GetAt(i).y;
        int x2 = pDoc->m_CenterCoord.GetAt(i+1).x;
        int y2 = pDoc->m_CenterCoord.GetAt(i+1).x;
        pDC->MoveTo(x1,y1);
        pDC->LineTo(x2,y2);

      }
 }
  
}

CMegaView::OnDraw(CDC*pDC)
{
   DrawData(CDC* pDC);
}



The problem here is that when running Release version,it works all right,while the Debug version encounters a Debug Assertion Failure.Debugger says there is a access violation,but I can''t see why and how to solve it.

解决方案

See Multithreading: Programming Tips[^] in the MSDN.

You did not tell us about your CMegaDoc class. If it is derived from CDocument or contains window handles, and calling member functions from the thread that access window handles, there will be assertions:

Access to a window from a thread that did not create the window is not supported by MFC. The debug assertions occur probably in CWnd::AssertValid(). See the source code in wincore.cpp and the comments about multithreaded applications.

If this happens with your application, you must change the design.


这篇关于Doc / View:将Doc指针传递给线程函数时遇到问题。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆