这是来自msdn的地铁相机应用程序。此代码用于显示相机的预览。相机列表将显示在组合框中。用户可以选择相机查看所选相机的预览,但是当我更换相机时,它首先释放资源而不是开始所选摄像头的预览。由于发布过程是异步过程并且它在后台运行,因此在发布之前,它会启动所选摄像机的预览,同时释放删除" m_MediaCaptureMgr"指针和程序崩溃。
在Win 32中,我可以使用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;
});
}
}