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).
source
share