Конкатенация задач в WinRT, почему нельзя использовать task::wait?

В моем текущем проекте у меня была недавняя проблема, и мне трудно ее понять:

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,
Спасибо

1 ответ

В C++/cx эта ошибка выдается, когда вы ждете в потоке пользовательского интерфейса, что вы не можете сделать. Но если вы просто добавляете документ xaml, вам не нужно ждать.

Другие вопросы по тегам