locked
check task exception

    Question

  • I have some codes like, I have questions inline:

     task< MyClass^ >( AsyncOp ).then( [ = ] ( MyClass^ obj )
        {
            do_something( obj );

        } ).then( [ = ]( task< void > prev )
        {
            try
            {
                prev.get( );  // What it checks? the result code of lambda which  contains do_something? do_something create other thread, I found this line is called before thread inside do_something completed. is it correct?
            }
            catch( Exception^ e )
            {
    // Sometimes INVALID PARAMETER exception is here, not always
            }
        } );
        
        
        void do_something( obj )
        {
          task< MyOtherClass^ >( AnotherAsyncOp ).then( [ = ] ( MyOtherClass^ obj )
            {
                // do something
        
            } ).then( [ = ]( task< void > prev )
            {
                try
                {
                    prev.get( );
                }
                catch( Exception^ e )
                {
        
                }
            } );
        }
        

    Overall, any problem with current codes? any idea to improve? thanks a lot!


    rob qqq

    Thursday, December 13, 2012 12:54 AM

All replies

  • As your do_something() creates another thread, so it is no longer at the current task chain. So your prev.get( )  has no effect on do_something() and your assigned behavior is correct. prev.get( ) only forces the asynchronous operation that you already assigned in the task chain.

    You can give do_something() in another task.

    In WinRT, task executes asynchronously and runs in background (separate worker thread). But when you use task chain like above,  task which included in a chain are execute sequentially. Such as myTask.then(…).then(…).then(…);

    As tasks are asynchronous they can not returns result immediately, rather than  they returns the way of result. But when you call prevTask.get(), It forces for result in non-blocking mode, whole task/task chain execution comparing with other operations are asynchronous.

    task<void>( yourTask ).then([this] (task<void> previousTask) {
            try {    
            //Task is executing........
            previousTask.get();
            //Task execution is done. This statement will be execute after the successful completion of task.    
            }
            catch (Exception^ exception) {  
            // Error in execution task.
            }
        });



    Thursday, December 13, 2012 2:39 AM
  • Thanks, I understand your sample code.  It is simple one. 

    So, nothing is wrong with my sample codes too? it creates another thread/task in  task continuation body.


    rob qqq

    Thursday, December 13, 2012 6:05 AM