在我当前的项目中,我最近遇到了一个问题并且无法理解它:
concurrency::create_task(BPClient->ReadAnswer()).then([this](Windows::Foundation::Collections::IVector<unsigned char>^ Vec) {
WSS::InMemoryRandomAccessStream^ imras = ref new WSS::InMemoryRandomAccessStream();
WSS::DataWriter^ dw = ref new WSS::DataWriter(imras->GetOutputStreamAt(0));
for(long long i = 0; i < Vec->Size; i++){
dw->WriteByte(Vec->GetAt(i));
}
concurrency::create_task(dw->StoreAsync()).then([this, imras](unsigned int Count){
Windows::UI::Xaml::Media::Imaging::BitmapImage^ bi = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage();
Image^ img = ref new Image();
bi->SetSource(imras);
img->Source = bi;
img->Width = 400;
img->Height = 400;
img->SetValue(Grid::ColumnProperty, 2);
concurrency::create_task(coredisp->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([=]() {
this->MainGrid->Children->Append(img);
})));
});
});
这工作正常并产生预期的结果。但是,如果我将其更改为
concurrency::create_task(BPClient->ReadAnswer()).then([this](Windows::Foundation::Collections::IVector<unsigned char>^ Vec) {
WSS::InMemoryRandomAccessStream^ imras = ref new WSS::InMemoryRandomAccessStream();
WSS::DataWriter^ dw = ref new WSS::DataWriter(imras->GetOutputStreamAt(0));
for(long long i = 0; i < Vec->Size; i++){
dw->WriteByte(Vec->GetAt(i));
}
concurrency::create_task(dw->StoreAsync()).wait(); //consider this line
Windows::UI::Xaml::Media::Imaging::BitmapImage^ bi = ref new Windows::UI::Xaml::Media::Imaging::BitmapImage();
Image^ img = ref new Image();
bi->SetSource(imras);
img->Source = bi;
img->Width = 400;
img->Height = 400;
img->SetValue(Grid::ColumnProperty, 2);
concurrency::create_task(coredisp->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal, ref new Windows::UI::Core::DispatchedHandler([=]() {
this->MainGrid->Children->Append(img);
}))).wait();
});
我最终收到一个错误,即无效参数已传递给最后一次concurrency::create_task
调用。
这里到底发生了什么?不能混合concurrency::task::then
和concurrency::task::wait
吗?我想我正在创建一个与使用concurrency::task::wait
而不是concurrency::task::then
.
谢谢