0

In my current project I have had a recent issue and have trouble understanding it:

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);
                })));
            });
        });

This works fine and produces the expected result. But, if I change it to

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();
        });

I eventually receive an error that an invalid parameter was passed to the last concurrency::create_task-call.

What is actually going on here? Is it not possible to mix concurrency::task::then and concurrency::task::wait? I think I am creating a similar task chain as when using concurrency::task::wait instead of concurrency::task::then.
Thank you

Charles
  • 50,943
  • 13
  • 104
  • 142
bash.d
  • 13,029
  • 3
  • 29
  • 42

1 Answers1

0

In c++/cx that error is thrown when you wait on the UI thread, which you can't do. But if you're just appending to the xaml document you shouldn't have to wait.

Matt
  • 1