如何重构大量使用 PPL 的代码。 C++

How to refactor code that is using PPL heavily. C++

所以我的函数看起来像这样

task<shared_ptr<myObjectsResult>> task1 = create_task([this,token, stream]
{
    // Here I have code that is working, but I would like to refactor it
    // maybe even make it go after or before this surrounding task.

    create_task(BitmapDecoder::CreateAsync(stream)).then([this, token]
    (BitmapDecoder^ bitmapDecoder)
    {
        create_task(bitmapDecoder->GetSoftwareBitmapAsync()).then([this, token]
        (SoftwareBitmap^ softwareBitmap)
        {
            OcrEngine^ ocrEngine = OcrEngine::TryCreateFromUserProfileLanguages();
            if (ocrEngine != nullptr)
            {
                create_task(ocrEngine->RecognizeAsync(softwareBitmap)).then([fileInfo, this, transactionPriority, token]
                (OcrResult^ ocrResult)
                {
                    doSomethingWithText(OcrResult->Text->Data());
                });
            }
        });
     });
    ...
    return runAsyncFunctionThatReturnsMyObjectResultTask(token);
});

它工作正常,一切都很好,但我想将 OCR 逻辑移动到代码的其他部分,但我很乐意从这里调用它。 我尝试过的是创建

task<OcrResult^> GetOCRTextFromStream(_In_ IRandomAccessStream^ stream)
{
    create_task(BitmapDecoder::CreateAsync(stream)).then([]
    (BitmapDecoder^ bitmapDecoder)
    {
         create_task(bitmapDecoder->GetSoftwareBitmapAsync()).then([]
         (SoftwareBitmap^ softwareBitmap)
         {
             OcrEngine^ ocrEngine = OcrEngine::TryCreateFromUserProfileLanguages();
         if (ocrEngine != nullptr)
         {
              return create_task(ocrEngine->RecognizeAsync(softwareBitmap));
         }
         else
         {
               OcrResult^ ocrResult = nullptr;
               return concurrency::task_from_result(ocrResult);
         }
     }
}

然后调用这个。

GetOCRTextFromStream(stream).then([this, token]
(OcrResult^ ocrResult)
{
    doSomethingWithText(OcrResult->Text->Data());
}

当然这行不通,但你得到了我想要的,我只是想重构这个,我只是不明白如何做我想做的,如果可行的话(我猜是吧?)

谢谢大家,对不起,如果我的问题是nooby :)

这是 C++/CX,解决方案是将 return.

如果您只需在您拥有的两个 create_task 前面添加 return,这应该有效

return create_task([]
{
    return create_task([]
    {
    ...
    }
}