C++ WinRT中的任务连接,为什么不能使用task::wait?
在我当前的项目中,我遇到了一个最近的问题,很难理解它:C++ WinRT中的任务连接,为什么不能使用task::wait?,c++,c++11,windows-runtime,concurrency-runtime,C++,C++11,Windows Runtime,Concurrency Runtime,在我当前的项目中,我遇到了一个最近的问题,很难理解它: concurrency::create_task(BPClient->ReadAnswer()).then([this](Windows::Foundation::Collections::IVector<unsigned char>^ Vec) { WSS::InMemoryRandomAccessStream^ imras = ref new WSS::InMemoryRandomAccessS
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())。然后([this](Windows::Foundation::Collections::IVector^Vec){
WSS::InMemoryRandomAccessStream ^imras=ref新建WSS::InMemoryRandomAccessStream();
WSS::DataWriter ^dw=ref新建WSS::DataWriter(imras->GetOutputStreamAt(0));
用于(长i=0;iSize;i++){
dw->WriteByte(Vec->GetAt(i));
}
并发::创建_任务(dw->StoreAsync())。然后([this,imras](unsigned int Count){
Windows::UI::Xaml::Media::Imaging::BitmapImage ^bi=ref新建Windows::UI::Xaml::Media::Imaging::BitmapImage();
图像^img=ref新图像();
bi->SetSource(imras);
img->Source=bi;
img->Width=400;
img->高度=400;
img->SetValue(Grid::ColumnProperty,2);
并发性::创建任务(coredisp->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal,ref新Windows::UI::Core::DispatchedHandler([=]){
此->主网格->子网格->附加(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(BPClient->ReadAnswer())。然后([this](Windows::Foundation::Collections::IVector^Vec){
WSS::InMemoryRandomAccessStream ^imras=ref新建WSS::InMemoryRandomAccessStream();
WSS::DataWriter ^dw=ref新建WSS::DataWriter(imras->GetOutputStreamAt(0));
用于(长i=0;iSize;i++){
dw->WriteByte(Vec->GetAt(i));
}
并发::创建_任务(dw->StoreAsync()).wait();//考虑这一行
Windows::UI::Xaml::Media::Imaging::BitmapImage ^bi=ref新建Windows::UI::Xaml::Media::Imaging::BitmapImage();
图像^img=ref新图像();
bi->SetSource(imras);
img->Source=bi;
img->Width=400;
img->高度=400;
img->SetValue(Grid::ColumnProperty,2);
并发性::创建任务(coredisp->RunAsync(Windows::UI::Core::CoreDispatcherPriority::Normal,ref新Windows::UI::Core::DispatchedHandler([=]){
此->主网格->子网格->附加(img);
}))).wait();
});
我最终收到一个错误,即向最后一个并发::create_task
-调用传递了一个无效参数
这里到底发生了什么?不能混合使用并发::任务::然后
和并发::任务::等待
?我想我正在创建一个类似于使用concurrency::task::wait
而不是concurrency::task::then
谢谢在c++/cx中,当您等待UI线程时会抛出错误,这是您无法做到的。但是,如果您只是附加到xaml文档,则不必等待。请发布您收到的确切错误消息。