2012-09-06 74 views
1

這是msdn的metro相機應用程序。此代碼用於顯示來自攝像機的預覽。攝像機列表將顯示在組合框中。用戶可以選擇攝像機以查看所選攝像機的預覽,但是當我更換攝像機時,它首先釋放資源,然後啓動由於發佈過程是異步過程,並且在後臺運行,所以在發佈之前,它會啓動所選攝像機的預覽,同時釋放刪除「m_MediaCaptureMgr」指針和程序崩潰。在Windows 8 metro應用程序中同步兩個ppl任務

在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; 
    }); 
} 
} 

回答

相關問題