在Windows 8 metro应用程序上同步两个ppl任务
Synchronizing two ppl task on Windows 8 metro app
这是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;
});
}
}
在WinRT中,您可以使用并发::事件类。方法事件::wait应该是WaitforSingleObject 的一个很好的替代品
相关文章:
- 如何在C++中从两个单独的for循环中添加两个数组
- 为什么两个不同的未命名名称空间可以共存于一个cpp文件中
- 当在同一名称空间中有两个具有相同签名的函数时,会发生什么
- 如何返回一个类的两个对象相加的结果
- 如何在C++中将一个无符号的 int 转换为两个无符号的短裤?
- 如何将两个不同矢量的同一位置的两个元素组合在一起
- 两个字符串在 c++ 中不相等
- 在两个类中共享相同的函数调用,并在不需要时避免空实例化
- 两个文件使用彼此的功能-如何解决
- 为什么Mat类的两个对象可以在不重载运算符+的情况下添加
- 如何确保在使用基于布尔值的两个方法之一调用方法时避免分支预测错误
- 停止cmake target_link_libraries将插件中静态库的两个对象文件链接到静态库本身
- 将fold表达式与std::一起用于两个元组
- 如何在C++中比较两个char数组
- 给定两个偶数,求出它们之间所有偶数的平方和
- 使用两个任务之间的静音信号
- 在Windows 8 metro应用程序上同步两个ppl任务
- 具有两个窗口的C++中的应用程序.任务栏中显示的一个窗口.另一个未显示
- 如何在intel TBB中同时运行两个不同的函数作为不同的任务
- 我有两个gpu,我怎么能让一个做特定的CUDA任务