Suggestion for a lot of chained Task I have a lot of Task continuation that depend on .Result of previous Task -> for example: I have a command that should:


Stop previous instance
Copy files elsewhere
Delete original files
Start instance again


So for example the command function:

public Task&lt;CommandResult&gt; Execute(ICommandArguments arguments)
{
    TaskCompletionSource&lt;CommandResult&gt; tcs = new TaskCompletionSource&lt;CommandResult&gt;();

    // 1. Stop
    var tskStop = this.stopCommand.Execute(arguments);
    tskStop.ContinueWith(stoppingTask =&gt;
    {
        if (stoppingTask.Result == CommandResult.Success)
        {
           // 2. Copy files
           var tskCopy = this.copyCommand.Execute(arguments);
           tskCopy.ContinueWith(copyingTask =&gt;
           {
               if (copyingTask.Result == CommandResult.Success)
               {
                  // 3. Delete
                  ... skip for brevity...
               }
               else
               {
                  // Could not 'delete'
                  tcs.SetResult(CommandResult.Fail);
                }
            }, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);

            tskCopy.ContinueWith(tskCopyBad =&gt;
            {
               //Error in 'copy'
               tcs.SetException(tskCopyBad.Exception.Flatten());
             }, TaskContinuationOptions.NotOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
         }
         else
         {
            // Could not 'stop' for some reason
            tcs.SetResult(stoppingTask.Result);
          }
      }, TaskContinuationOptions.OnlyOnRanToCompletion | TaskContinuationOptions.AttachedToParent);

      tskStop.ContinueWith(tskSTopBad =&gt;
      {
           // Exception in 'stop'
           tcs.SetException(tskStopBad.Exception.Flatten());
      }, TaskContinuationOptions.NotOnRanToCompletion | TaskContinuationOptions.AttachedToParent);
}


and you see that only with 2 'commands' code blown to so many lines. Is there any design pattern, shortcut, or extension that I can add/use to shorten code lines? I depend on the .Result from the previous Task and its Status (to know whether an exception was thrown or not).

Note that we currently develop our app in .NET 4 so we are limited to Task, and unfortunately cannot use async/await.

P.S. All those copyCommand, deleteCommand is also have a public Task&lt;CommandResult&gt; Execute() function
You may not be able to get away from chaining a ton of ContinueWith invocations together. However, you might be able to create a builder class with methods that you pass parameters to so you don't have to call ContinueWith from calling code. 

It looks like you might be able to remove a lot of duplication by extracting your anonymous methods into one or more stateless (probably static) methods that you can reuse by passing the method signature in, e.g. 

private static void SetFlattenedException(Task stoppedTask, TaskContinuationOptions options)


You can also start going down the route of wrapping continuations in method calls with additional Action parameters to "inject" code to execute, but given that it tends to lead to a lot of "inside-out" code that is hard to follow, I'd use sparingly.