VC++ MFC 对话框控件里,将一个响应函数通过多线程编程程序就执行不了了。请问是什么原因呢?
本来响应函数为:
[code=C/C++][/code]
void CDlg::function()
{
CvCapture* capture = 0;
capture = cvCaptureFromCAM(0);
if( !capture )
{
fprintf(stderr,"Could not initialize capturing...\n");
}
for(;;)
{
IplImage* frame = 0;
frame = cvQueryFrame( capture );
if( !frame )
break;
CDC *pDC = GetDlgItem(IDC_STATIC)->GetDC();
CvvImage cimg;
HDC hDC= pDC->GetSafeHdc();
CRect rect;
GetDlgItem(IDC_STATIC)->GetClientRect(&rect);
cimg.CopyOf(frame); //gFrame是那帧图像
cimg.DrawToHDC(hDC,&rect);
ReleaseDC(pDC);
}
cvReleaseCapture( &capture );
}
为了用多线程编程,我将它写在线程函数中,(为了能用全局函数,我定义了 CVideo dlg,也不知道是否正确)如下:
[code=C/C++][/code]
DWORD WINAPI LoadVideo(LPVOID pParam)
{
CVideoDlg dlg;
CvCapture* capture = 0;
capture = cvCaptureFromCAM(0);
if( !capture )
{
//fprintf(stderr,"Could not initialize capturing...\n");
AfxMessageBox("Could not initialize capturing...");
}
for(;;)
{
IplImage* frame = 0;
frame = cvQueryFrame( capture );
if( !frame )
break;
CDC *pDC = dlg.GetDlgItem(IDC_STATIC)->GetDC();
CvvImage cimg;
HDC hDC= pDC->GetSafeHdc();
CRect rect;
dlg.GetDlgItem(IDC_STATIC)->GetClientRect(&rect);
cimg.CopyOf(frame); //gFrame是那帧图像
cimg.DrawToHDC(hDC,&rect);
dlg.ReleaseDC(pDC);
}
cvReleaseCapture( &capture );
return 1;
}
然后再通过CreateThread(或者AfxBeginThread函数,都用过了,不行)调用,结果编译没问题,但是点击按钮的时候弹出错误。不能运行。
由于不能上传图片,我描述下:
Debug Assertion Failed!
Program:......
File:winocc.cpp
Line:76
For information on how your program can cause anassertion failure,see the ......
[解决办法]
1. 窗口对象不允许跨线程使用,所以你的目的无法实现
2. 在线程内部定义的那个dlg和你实际想操作的dlg毫无关系,楼主关于“实例”的概念还是需要搞搞
[解决办法]