【问题标题】:How do you access a variable returned from a previous task in a chain?如何访问从链中上一个任务返回的变量?
【发布时间】:2012-12-14 12:40:29
【问题描述】:

我有以下任务链,我想在最后一个任务中访问变量decoder,但在更早的任务中创建。

create_task(file->OpenReadAsync()).then([](IRandomAccessStream^ inStream) {
    return BitmapDecoder::CreateAsync(inStream);

}).then([localFolder](BitmapDecoder^ decoder) {
    return localFolder->CreateFileAsync("map.png", CreationCollisionOption::ReplaceExisting);

}).then([](StorageFile^ outFile) {
    return outFile->OpenAsync(FileAccessMode::ReadWrite);

}).then([](IRandomAccessStream^ outFileStream) {
    return BitmapEncoder::CreateAsync(BitmapEncoder::PngEncoderId, outFileStream);

}).then([](BitmapEncoder^ encoder) {
    BitmapPixelFormat pixelFormat = decoder->BitmapPixelFormat; // how do I make decoder available here?
    // Do stuff that uses encoder & decoder
});

【问题讨论】:

  • 1.这伤害了我的眼睛,而且 2. 这不是 C++,它是 C++/CLI
  • 有什么建议可以减少对眼睛的伤害吗?
  • 你认为这样链接它的好处是什么?为什么不把所有东西都放在一个函数中,然后在任务中调用该函数呢?
  • 它们是异步操作,没有等效的同步函数,我认为最好的做法是链接任务而不是嵌套它们。
  • C++ 人群和 Ed 的女孩疯狂的代码,它是 C++/CX 代码。这不会阻止您将中间值存储在类的字段中,以便您可以稍后访问它。

标签: windows-8 c++-cx


【解决方案1】:

最后我只是将我需要的变量分配给包含任务链的函数的本地变量。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-07-27
    相关资源
    最近更新 更多