是否可以在MFC中的另一个线程内创建一个线程?

时间:2015-06-25 17:25:06

标签: c++ multithreading opencv visual-studio-2012 mfc

我在使用opencv的MFC中有一个关于线程的问题。让我先描述一下我的问题。我有一个用于显示摄像机视频帧的GUI。因此,我必须使用一个线程从相机获取视频并将其显示到GUI。它完成了。但是,我想扩展我的问题,例如:当视频显示时,我想通过命令在opencv的其他窗口中显示该视频

IplImage* image2=cvCloneImage(&(IplImage)original);
cvShowImage("Raw Image", image2);
cvReleaseImage(&image2);

据我所知,我需要在线程中创建一个新线程从相机获取视频。有可能吗?让我们看看我的代码,你能给我一些解决方案或建议来完成这项任务吗?非常感谢你

这是我的代码

THREADSTRUCT *_param = new THREADSTRUCT;
_param->_this = this;
CWinThread* m_hThread;
m_hThread = AfxBeginThread (StartThread, _param);

在StartThread功能中,我将从摄像机调用加载视频,例如

UINT Main_MFCDlg::StartThread (LPVOID param)
{
    THREADSTRUCT*    ts = (THREADSTRUCT*)param;
    cv::VideoCapture cap;
    cap.open(0);
    while (true)
    {
    Mat frame;
    Mat original;
    cap >> frame;
    if (!frame.empty()){
        original = frame.clone();           
        //Display video in GUI
        CDC* vDC_VIDEO;
        vDC_VIDEO=ts->_this->GetDlgItem(IDC_VIDEO)->GetDC();
        CRect rect_VIDEO;
        ts->_this->GetDlgItem(IDC_VIDEO)->GetClientRect(&rect_VIDEO);

        //Is it possible to create a thread in here to show video with other
        //delay time such as 1000ms
        //To call the function cv::imshow("Second window", original);

        }
    if (waitKey(30) >= 0) break;// Delay 30ms for first window
    }
}

注意线程结构看起来像

//structure for passing to the controlling function
typedef struct THREADSTRUCT
{
    Main_MFCDlg*    _this;
} THREADSTRUCT;

1 个答案:

答案 0 :(得分:0)

Of course you can create a thread in a thread. You problem is: the worker thread should not access UI object directly in the UI thread, the details are explained here In your worker thread, after the background job is done, you can use SendMessage to send an message to the GUI and let it update.