locked
Visual C++ WinRT create_task on IAsyncOpperation<Array<>>

    Question

  • I am creating an IAsyncAction<Array<unsigned char>^> via the create_async function.  When I then take the result of the opperation and put it into a create_task block I get a compiler error.  Anyone have any ideas why this occurs.  If my create_async function returns just a single unsigned char, then there is not an issue.

    Here is my code:

            Array<unsigned char>^ arr;
    	auto opp = create_async([this]() -> Array<unsigned char>^
    	{
    		Array<unsigned char>^ a = ref new Array<unsigned char>(30);
    		return a;
    	});
    	create_task(opp);

    Here is the compiler error I get:

    :\program files (x86)\microsoft visual studio 11.0\vc\include\ppltasks.h(3540): error C2664: 'void Concurrency::task<_ReturnType>::_TaskInitNoFunctor(Concurrency::task_completion_event<_ResultType> &)' : cannot convert parameter 1 from 'Concurrency::details::_AsyncTaskGeneratorThunk<_Function> ' to 'Concurrency::task_completion_event<_ResultType> &'
    1>          with
    1>          [
    1>              _ReturnType=Platform::Array<unsigned char,1> ^,
    1>              _ResultType=Platform::Array<unsigned char,1> ^
    1>          ]
    1>          and
    1>          [
    1>              _Function=method::<lambda_6bb4ca4120feca3477b5068449426141>
    1>          ]
    1>          and
    1>          [
    1>              _ResultType=Platform::Array<unsigned char,1> ^
    1>          ]
    1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ppltasks.h(2717) : see reference to function template instantiation 'void Concurrency::task<_ReturnType>::_TaskInitMaybeFunctor<_Ty>(_Ty &,std::false_type)' being compiled
    1>          with
    1>          [
    1>              _ReturnType=Platform::Array<unsigned char,1> ^,
    1>              _Ty=Concurrency::details::_AsyncTaskGeneratorThunk<method::<lambda_6bb4ca4120feca3477b5068449426141>> ^
    1>          ]
    1>          c:\program files (x86)\microsoft visual studio 11.0\vc\include\ppltasks.h(4215) : see reference to function template instantiation 'Concurrency::task<_ReturnType>::task<_Ty>(_Ty)' being compiled
    1>          with
    1>          [
    1>              _ReturnType=Platform::Array<unsigned char,1> ^,
    1>              _Ty=Concurrency::details::_AsyncTaskGeneratorThunk<method::<lambda_6bb4ca4120feca3477b5068449426141>> ^
    1>          ]
    1>          c:\a.cpp(143) : see reference to function template instantiation 'Concurrency::task<_ReturnType> Concurrency::create_task<Concurrency::details::_AsyncTaskGeneratorThunk<_Function>^>(_Ty)' being compiled
    1>          with
    1>          [
    1>              _ReturnType=Platform::Array<unsigned char,1> ^,
    1>              _Function=method::<lambda_6bb4ca4120feca3477b5068449426141>,
    1>              _Ty=Concurrency::details::_AsyncTaskGeneratorThunk<method::<lambda_6bb4ca4120feca3477b5068449426141>> ^
    1>          ]

    Monday, July 09, 2012 9:38 PM

All replies

  • Hi Michael,

    So, from the documentation of create_async the return value should be one of
    the following :-

     

    IAsyncAction^

    IAsyncActionWithProgress<TProgress>^

    IAsyncOperation<TResult>^

    IAsyncOperationWithProgress<TResult, TProgress>^

     

    From thedefinition of the task, the task was returning Array<unsigned char>^ which is not listed above. So, we need to have Array<unsigned char>^ in place of one of the <TResult> above.

     

    Hence the code should be :-

     

    //Definition :-

     

    IAsyncOperation<Array<unsigned char>^>^
    GetArrayAsync()
    {
     return create_async([this](){
     return
    getArray();
     
     });
    }

     

    Array<unsigned char>^ getArray()
    {
    Array<unsigned char>^ a
    = ref new Array<unsigned char>(30);
    return a;

    }

     

     

    //How to invoke :-

        
    IAsyncOperation<Array<unsigned char>^>^ operation =
    GetArrayAsync();

    operation -> Completed = ref new
    AsyncOperationCompletedHandler<Array<unsigned
    char>^>([=](IAsyncOperation<Array<unsigned char>^>^
    result,AsyncStatus stat)
    {
    //do the operation on result here

    });

     

    -Sagar

    Tuesday, July 10, 2012 2:03 PM
    Moderator
  • Thanks for the reply.  However is there any way to do this with tasks?
    Friday, July 13, 2012 5:13 PM
  • Yes.

    task<Array<unsigned char>^>^ t = create_task(GetArrayAsync());

    t.then( [](Array<unsigned char>^>^ array) { /* do the operation on result here */ } );

    -Sagar


    Friday, July 13, 2012 5:48 PM
    Moderator