2017-08-03 28 views
-1

所以我有功能,看起來像這樣如何重構大量使用PPL的代碼。 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()); 
} 

Ofcourse,這並不工作,但你得到我想要什麼,我只是想重構這個,我只是不明白怎麼做我想做的,如果它是可行的(我猜它是什麼? )

感謝所有和抱歉,如果我的問題是nooby :)

+0

這是C++/CLI(或本週管它叫什麼),而不是C++。 – molbdnilo

回答

0

這是C++/CX和解決方案是把回報。

這應該工作,如果你只是兩個create_task前加上回報,你有

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