0

在我当前的项目中,我最近遇到了一个问题并且无法理解它:

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::thenconcurrency::task::wait吗?我想我正在创建一个与使用concurrency::task::wait而不是concurrency::task::then.
谢谢

4

1 回答 1

0

在 c++/cx 中,当您在 UI 线程上等待时会引发该错误,这是您无法做到的。但是,如果您只是附加到 xaml 文档,则不必等待。

于 2013-10-30T18:32:30.727 回答