基本上我有下一个函数需要调用异步:
void waitForFrames(){
CMFTWrapper::IsRunning = true;
while (CMFTWrapper::IsRunning){
result = WaitForSingleObjectEx(CMFTWrapper::FrameEvent, INFINITE, true);
if (result != WAIT_OBJECT_0){
// capture aborted, quit.
}
else if (CMFTWrapper::count > 0){
// copy the bitmap data
}
}
}
现在我尝试这样做:
create_task(waitForFrames())
.then([this](task<void> frameTask)
{
XTRACE(L"=================================FINISHED WITH FRAME TASKn");
});
这给了我下一个错误:
Error 26 error C2228: left of '.then' must have class/struct/union C:UsersAlin RosuWorkspacevidyomobile_windows_phoneVidyo.DeviceManagerWinRTDeviceDetectionLmiVideoCapturerWinRTImplementation.cpp 181 1 Vidyo.DeviceManager
Error 24 error C2784: 'Concurrency::task<_Ty> Concurrency::create_task(const Concurrency::task<_Ty> &)' : could not deduce template argument for 'const Concurrency::task<_Ty> &' from 'void' C:UsersAlin RosuWorkspacevidyomobile_windows_phoneVidyo.DeviceManagerWinRTDeviceDetectionLmiVideoCapturerWinRTImplementation.cpp 180 1 Vidyo.DeviceManager
Error 25 error C2784: 'Concurrency::task<details::_TaskTypeFromParam<_Ty>::_Type> Concurrency::create_task(_Ty,Concurrency::task_options)' : could not deduce template argument for '_Ty' from 'void' C:UsersAlin RosuWorkspacevidyomobile_windows_phoneVidyo.DeviceManagerWinRTDeviceDetectionLmiVideoCapturerWinRTImplementation.cpp 180 1 Vidyo.DeviceManager
Error 45 error LNK1104: cannot open file 'C:UsersAlin RosuWorkspacevidyomobile_windows_phoneBuildARMReleaseVidyo.DeviceManagerLmiDeviceManagerWinRT.lib' C:UsersAlin RosuWorkspacevidyomobile_windows_phoneVidyo.DeviceManager.TestLINK Vidyo.DeviceManager.Test
现在我尝试将函数的返回值从 void(Dword、int)更改为其他内容,但仍然遇到类似的错误。查看我在网上找到的示例,我找到的所有使用它的函数都返回 IAsyncAction。例:
create_task(m_pMediaCapture->StartRecordToStorageFileAsync(m_EncodingProfile, m_recordStorageFile))
.then([this](task<void> recordTask)
{
XTRACE(L"=================================will try to get record taskn");
}
如何使用我的正常函数执行此操作,以便它可以异步运行?
您可以将 lambda 直接传递给 create_task 函数:
create_task( [](){ /* code here */ } )
.
因此,在您的方案中,以下内容应该有效:
create_task([](){ waitForFrames(); })
.then([this](task<void> frameTask){
XTRACE(L"=================================FINISHED WITH FRAME TASKn");
});