Task.ContinueWith and TaskContinuationOptions.AttachedToParent

I am trying to use Task.ContinueWithc TaskContinuationOptions.AttachedToParentto perform a continuation task, for example.

Task outerTask = new Task(() => Console.WriteLine("Outer"));
Task innerTask = outerTask.ContinueWith(t =>
    {
        Thread.Sleep(500);
        Console.WriteLine("Inner");
    }, 
    TaskContinuationOptions.AttachedToParent | TaskContinuationOptions.OnlyOnRanToCompletion);
outerTask.Start();
outerTask.Wait();
Console.WriteLine("Outer done");

Exit from this code

Outer
Outer done
Inner

but what I expected / trying to achieve

Outer
Inner
Outer done

Can it be used AttachedToParentto block external completion of a task or is it AttachedToParenteffective only for nested tasks?

Edit:

What I'm trying to achieve is that I want to start the continue task when it is outerTasksuccessfully completed. I want to outerTaskthrow exceptions from the innerTaskcaller without having to deal with innerTask. If it outerTaskfails, it should just throw an exception right away (on outerTask.Wait()no call innerTask).

+5
source share
3

- .
, , :

  • , .. ,
  • , , ,

    " AttachedToParent ..."

    " , externalTask    "

    CiCiting :

    • " , , , . Wait
    • , , . . , AggregateException. "

, - , Continuations:

" , , , . , , , . , . - , Wait Try/Catch"

, ,

:

Edit:

, , - , externalTask ​​ . , externalTask innerTask , innerTask. externalTask ​​ , ( externalTask.Wait() innerTask).

, MSDN Task.ContinueWith Method (Func, CancellationToken, TaskContinuationOptions, TaskScheduler), / , / ( , 4 ). parent-child, -, .

, , (, , ).

+7

,

outerTask.Wait();

innerTask.Wait();

, Wait "Outer done", - Thread.Sleep.

+1

, "". "" , ( ) .

-1

All Articles