C++ 在Windows 8 metro应用程序上同步两个ppl任务

C++ 在Windows 8 metro应用程序上同步两个ppl任务,c++,visual-c++,microsoft-metro,windows-runtime,winrt-async,C++,Visual C++,Microsoft Metro,Windows Runtime,Winrt Async,这是msdn的metro camera应用程序。此代码用于显示摄像头的预览。此处,组合框中将显示摄像头列表。用户可以选择摄像头以查看所选摄像头的预览,但是,当我更改相机时,它首先释放资源,然后启动选定相机的预览。由于发布过程是异步过程,并且在后台运行,因此在发布之前,它会启动选定相机的预览,同时在发布时删除“m_MediaCaptureMgr”指针,程序会崩溃 在Win32中,我可以使用waitforSingle对象来同步它。我想知道如何在WinRT和ppl任务中最好地同步 void Camer

这是msdn的metro camera应用程序。此代码用于显示摄像头的预览。此处,组合框中将显示摄像头列表。用户可以选择摄像头以查看所选摄像头的预览,但是,当我更改相机时,它首先释放资源,然后启动选定相机的预览。由于发布过程是异步过程,并且在后台运行,因此在发布之前,它会启动选定相机的预览,同时在发布时删除“m_MediaCaptureMgr”指针,程序会崩溃

在Win32中,我可以使用waitforSingle对象来同步它。我想知道如何在WinRT和ppl任务中最好地同步

void CameraApp::MainPage::cmbCameraSelector_SelectionChanged(Platform::Object^ sender,  Windows::UI::Xaml::Controls::SelectionChangedEventArgs^ e)
{
if(m_DeviceVector.size() > 0)
{
    m_CaptureInitSettings->VideoDeviceId = m_DeviceVector[cmbCameraSelector->SelectedIndex]->Id;
    InitMediaCapture();
}
}

void CameraApp::MainPage::InitMediaCapture()
{
ReleaseMediaCapture();

//Sleep(3000);
auto _this = this;
m_MediaCaptureMgr = ref new MediaCapture();

task<void> stratPreview(m_MediaCaptureMgr->InitializeAsync(m_CaptureInitSettings));
stratPreview.then([_this]
{
    _this->previewElement->Source = _this->m_MediaCaptureMgr;
    task<void> startPrev(_this->m_MediaCaptureMgr->StartPreviewAsync());
    startPrev.then([=]
    {

        return _this->GetCameraSettings();
    });     
});

}    
void CameraApp::MainPage::ReleaseMediaCapture()
{
if (m_MediaCaptureMgr )
{
    auto prevOp = m_MediaCaptureMgr->StopPreviewAsync();
    task<void> releaseMediaCapture(m_MediaCaptureMgr->StopPreviewAsync());
    releaseMediaCapture.then([=]
    {
        m_MediaCaptureMgr = nullptr;
        bRelease = false;
    });
}
}
void CameraApp::MainPage::cmbCameraSelector\u SelectionChanged(平台::对象^sender,Windows::UI::Xaml::控件::SelectionChangedEventArgs ^e)
{
如果(m_DeviceVector.size()>0)
{
m_CaptureInitSettings->VideoDeviceId=m_DeviceVector[cmbCameraSelector->SelectedIndex]->Id;
InitMediaCapture();
}
}
void CameraApp::MainPage::InitMediaCapture()
{
ReleaseMediaCapture();
//睡眠(3000);
自动_this=this;
m_MediaCaptureMgr=ref new MediaCapture();
任务预览(m_MediaCaptureMgr->InitializeAsync(m_CaptureInitSettings));
stratPreview.然后([[u this]
{
_this->previewElement->Source=\u this->m\u MediaCaptureMgr;
任务startPrev(_this->m_MediaCaptureMgr->StartPreviewSync());
startPrev.then([=]
{
返回_this->GetCameraSettings();
});     
});
}    
void CameraApp::MainPage::ReleaseMediaCapture()
{
if(m_MediaCaptureMgr)
{
auto prevOp=m_MediaCaptureMgr->StopReviewAsync();
任务释放MediaCapture(m_MediaCaptureMgr->StopReviewAsync());
释放MediaCapture。然后([=]
{
m_MediaCaptureMgr=nullptr;
bRelease=false;
});
}
}
在WinRT中,您可以使用类。这种方法应该是一种很好的替代方法