【发布时间】: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 代码。这不会阻止您将中间值存储在类的字段中,以便您可以稍后访问它。