C++/Cx 是否可以使用 String 而不是 IAsyncAction 调用 CreateTask().then?

C++/Cx is it possible to call CreateTask().then with a String instead of a IAsyncAction?

基本上我有下一个需要调用 Async 的函数:

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 TASK\n");
        });

这给了我下一个错误:

Error   26  error C2228: left of '.then' must have class/struct/union   C:\Users\Alin Rosu\Workspace\vidyomobile_windows_phone\Vidyo.DeviceManager\WinRT\DeviceDetection\LmiVideoCapturerWinRTImplementation.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:\Users\Alin Rosu\Workspace\vidyomobile_windows_phone\Vidyo.DeviceManager\WinRT\DeviceDetection\LmiVideoCapturerWinRTImplementation.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:\Users\Alin Rosu\Workspace\vidyomobile_windows_phone\Vidyo.DeviceManager\WinRT\DeviceDetection\LmiVideoCapturerWinRTImplementation.cpp    180 1   Vidyo.DeviceManager
Error   45  error LNK1104: cannot open file 'C:\Users\Alin Rosu\Workspace\vidyomobile_windows_phone\Build\ARM\Release\Vidyo.DeviceManager\LmiDeviceManagerWinRT.lib'    C:\Users\Alin Rosu\Workspace\vidyomobile_windows_phone\Vidyo.DeviceManager.Test\LINK    Vidyo.DeviceManager.Test

现在我尝试将函数的 return 值从 void (Dword, int) 更改为其他内容,但我仍然遇到类似的错误。 查看我在网上找到的示例,所有使用它的函数,我发现 return 返回 IAsyncAction。 示例:

create_task(m_pMediaCapture->StartRecordToStorageFileAsync(m_EncodingProfile, m_recordStorageFile))
                    .then([this](task<void> recordTask)
                {
                    XTRACE(L"=================================will try to get record task\n");
}

如何使用我的正常功能执行此操作,以便它可以异步运行?

您可以将 lambda 直接传递给 create_task 函数:

create_task( [](){ /* code here */ } ).

所以在您的场景中,以下应该有效:

create_task([](){ waitForFrames(); })
.then([this](task<void> frameTask){
            XTRACE(L"=================================FINISHED WITH FRAME TASK\n");
});