Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

ContinueWith Vs await

0.00/5 (No votes)
11 Aug 2015 1  
Below discussion about the ContinueWith function available on Task Class of TPL and await keyword introduced in C# 5.0 to support asynchronous calls.

TPL is new library introduced in C # 4.0 version to provide good control over thread, to make use of multicore CPU by mean of parallel execution on thread. Below discussion is not about TPL but its about ContinueWith function available on Task Class of TPL and await keyword introduced in C# 5.0 to support asynchronous calls.

ContinueWith - its method available on the task which allows executing code after task finished execution. In simple word it allows continuation.

Things to note here is ContinueWith also return one Task. That means you can attach ContinueWith on task return by this method.

Example :

public void ContinueWithOperation()
{
   Task<string> t = Task.Run(() => LongRunningOperation("Continuewith", 500));
   t.ContinueWith((t1) =>
   {
       Console.WriteLine(t1.Result);
   });
}

In above code new created task runs LongRunningOperation and once task execution completed ContinueWith execute operation on retuned task and print result of task.

ContinueWith operation thask get executed by default thread scheduler, one can also provide other scheduler for running task on it, this discussed in later in this article.

Note: below code is LongRunningOperation called by task. LongRunningOpertion here is just example in real program one cannot call long running operation on task, and if one want to call longrunning task than one need to pass TaskCreationOperation.LongRunning.

private string LongRunningOperation(string s, int sec)
{
  Thread.Sleep(sec);
  return s + " Completed";
}

await – its keyword that cause runtime to run operation on new task and cause executing thread to return and continue with execution (In most of the cases executing that is main thread of application). Once await operation get finished it returns to statement where is left of (i.e. returns to caller i.e. it returns according state saved) and start executing statements.

So await wait for newly created task to finish and ensures continuation once execution of waiting task is finished.

await keyword used with async to achieve asynchronous programming in C#. It’s called asynchronous programming because runtime capture state of program when it encounter await keyword (which is similar to yield in iterator) and restore state back once waited task finish so the continuation runs on correct context.

Example :

public async void AsyncOperation()
{
    string t = await Task.Run(() => LongRunningOperation("AsyncOperation", 1000));
    Console.WriteLine(t);
}

In above example new created task calls LongRunningOperation and execution left of once main thread encounter await keyword i.e. main thread return to caller of AsyncOpetion and execute further. Once LongRunningOpertaion completed than result of task get printed on console.

So here because state saved when await encountered flow returns on same context one operation on task executed.

Note: State is having detail about executioncontext/synchronizationcontext.

So form above its clear that both Task.ContinueWith and await Task wait for task to finish and allows continuation after task completion. But both works differently.

Difference between ContinueWith and await

  1. Saving Sate for And return on Execution context

    ContinueWith doesn’t save any kind of state, continuation operation attached using ContinueWith run on default thread scheduler in case not scheduler provided.

    await – on encounter of this keyword state get saved and once task on which await done get completed execution flow pick up saved state data and start execution statement after await. (Note: State is having detail about executioncontext/synchronizationcontext.)

  2. Posting Completed Task result on UI Control

    Blow is example with ContinueWith and await to display completion task result on UI control.

    ContinueWith :

    Consider below code which display result of completed task on UI label.

    public void ContinueWithOperation()
           {
             CancellationTokenSource source = new CancellationTokenSource();
             source.CancelAfter(TimeSpan.FromSeconds(1));
             Task<string> t = Task.Run(() => LongRunningOperation("Continuewith", 500
    ));
    
                t.ContinueWith((t1) =>
                {
                    if (t1.IsCompleted && !t1.IsFaulted && !t1.IsCanceled)
                        UpdateUI(t1.Result);
                });
    }
    
    private void UpdateUI(string s)
           {
             label1.Text = s;
    }

    Note : LogRunningOperation is already given above.

    When above code is get executed below runtime exception occurs.

    This exception occurs because Continuation operation UpdateUI operation runs on different thread, in this case thread will be provided by default threadschedular which is ThreadPool and it doesn’t have any information about Synchronization context on which to run.

    To avoid exception, one must need to pass thread scheduler which pass data on UI SynchronizationContenxt. In below code TaskScheduler.FromCurrentSynchronizationContext() method pass UI related thread scheduler.

    t.ContinueWith((t1) =>
    
            {
    
                if (t1.IsCompleted && !t1.IsFaulted && !t1.IsCanceled)
    
                    UpdateUI(t1.Result);
    
            }, TaskScheduler.FromCurrentSynchronizationContext());
    

    await :

    Consider below code which display result of completion on UI.

    public async void AsyncOperation()
    {
        try
        {
           string t = await Task.Run(() => LongRunningOperation("AsyncOperation",
                            10000));
           UpdateUI(t);
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }
    }

    Code written with await doesn’t throw any exception when posting data to UI control, No special code required for await. This happens because as discussed in 1 point of difference , on encounter of await state data get saved which has information about SynchronizationContext.

    So if one need to post data on UI than await is good option because there is no need of extra care/code to post data on UI.

  3. Handling Exception and Cancellation

    Consider below example code for ContinueWith and await method for handing exception and cancelled task.

    ContinueWith

    Below is example code how the exception/cancellation handled using ContinueWith.

    public void ContinueWithOperationCancellation()
           {
                CancellationTokenSource source = new CancellationTokenSource();
                source.Cancel();
    
                Task<string> t = Task.Run(() =>
                    LongRunningOperationCancellation("Continuewith", 1500,
                        source.Token), source.Token);
    
                t.ContinueWith((t1) =>
                {
                    if (t1.Status == TaskStatus.RanToCompletion)
                        Console.WriteLine(t1.Result);
                    else if (t1.IsCanceled)
                        Console.WriteLine("Task cancelled");
                    else if (t.IsFaulted)
                    {
                        Console.WriteLine("Error: " + t.Exception.Message);
                    }
                });
    }

    In above code cancellation/exception in continuation handled with by using TaskStatus. Other way to do same thing by making use of TaskContinuationOptions.OnlyOnRanToCompletion

    t.ContinueWith(
        (antecedent) => {  },
         TaskContinuationOptions.OnlyOnRanToCompletion);

    await

    Below is example code how the exception/cancellation handled using await.

    public async void AsyncOperationCancellation()
           {
             try
             {
               CancellationTokenSource source = new CancellationTokenSource();
               source.Cancel();
               string t = await Task.Run(() =>
                   LongRunningOperationCancellation("AsyncOperation", 2000, source.Token),
                       source.Token);
                    Console.WriteLine(t);
             }
             catch (TaskCanceledException ex)
             {
                    Console.WriteLine(ex.Message);
             }
             catch (Exception ex)
             {
                    Console.WriteLine(ex.Message);
             }
    }

    Above code doesn’t make use of task status as continuewith, it makes use of try ..catch block to handle exception. To handle cancellation there is need of catch block with TaskCanceledException.

    So from above example my view is cancellation/exception handling is done very clean way when one use continuation.

    Below is code for LongRunningOperationCancellation method.

    private string LongRunningOperationCancellation(string s, int sec,
    CancellationToken ct)
           {
             ct.ThrowIfCancellationRequested();
             Thread.Sleep(sec);
             return s + " Completed";
    }

    Above three cases shows difference of coding between ContinueWith and await. But below one shows why await is better than ContinueWith.

  4. Complex flow

    Consider below function which used for calculation factorial of number

    public KeyValuePair<int, string> Factorial(int i)
    
         {
    
             KeyValuePair<int, string> kv;
    
             int fact = 1;
    
             for (int j = 1; j <= i; j++)
    
                 fact *= j;
    
             string s = "factorial no " + i.ToString() + ":" + fact.ToString();
    
             kv = new KeyValuePair<int, string>(i, s);
    
             return kv;
    
         }
    

    Above function calculate factorial of number and return KeyValuePair to caller to display calculation.

    Now problem statement is above function to calculate factorial of number 1 to 5.

    ContinueWith

    Below is code to calculate factorial of numbers between 1 to 5. Expectation from the code below is calculating factorial of number, display it in order and once it completed “Done” message will get printed on the console.

    public void ContinueWithFactorial()
           {
                for (int i = 1; i < 6; i++)
                {
                    int temp = i;
                    Task<KeyValuePair<int, string>> t = Task.Run(() => Factorial(temp));
                    t.ContinueWith((t1) =>
                    {
                        KeyValuePair<int, string> kv = t1.Result;
                        Console.WriteLine(kv.Value);
                    });
                }
                Console.WriteLine("Done");
    }

    Once code get executed one will find that “Done” message get printed immediately i.e. first than factorial of number get displayed on screen. One more problem is number factorial is not get displayed in order. Below is output of the code execution.

    So to resolve problem with above code , code need to be refactored like this

    public void FactContinueWithSeq(int i)
    {
                Task<KeyValuePair<int, string>> t = Task.Run(() => Factorial(i));
                var ct = t.ContinueWith(((t1) =>
                {
                    KeyValuePair<int, string> kv = t1.Result;
                    int seed = kv.Key;
                    if (seed < 6)
                    {
                        Console.WriteLine(kv.Value);
                        seed++;
                        FactContinueWithSeq(seed);
                    }
                    else
                    {
                        Console.WriteLine("Done");
                        return;
                    }
                }));
    }

    Above function will be called like this p.FactContinueWithSeq(1).

    In above code to maintain sequence, task need to be fired one by one. And for that once one task completes its execution Contuation on it with the help of ContinueWith method call function again. It’s like doing recursive calling to function.

    And to display “Done” message at the end need to check for the seed to function. Which checks seed value increases 6 or not.

    But now there is need for attaching continuation on completion of FactContinueWithSeq, to achieve this code need to be done as below.

    TaskCompletionSource<string> tcs = new TaskCompletionSource<string>();
    
    public Task<string> FactAsyncTask { get { return tcs.Task; } }
    
    public void FactContinueWithSeqAsync(int i)
    
    {
    
        Task<KeyValuePair<int, string>> t = Task.Run(() => Factorial(i));
    
        var ct = t.ContinueWith(((t1) =>
    
        {
    
            KeyValuePair<int, string> kv = t1.Result;
    
            int seed = kv.Key;
    
            if (seed < 5)
    
            {
    
                Console.WriteLine(kv.Value);
    
                seed++;
    
                FactContinueWithSeqAsync(seed);
    
            }
    
            else
    
            {
    
                tcs.SetResult("Execution done");
    
            }
    
        }));
    
    }
    

Call to above function

p.FactContinueWithSeqAsync(1);

Task<string> t = p.FactAsyncTask;

t.ContinueWith((t1)=> Console.WriteLine(t.Result));

In above code TaskCompletionSource is used to achieve the code of providing continutation on completion of factorial calculation.

So one need to do lot of code for providing expected result, which is calculating factorial in sequence, waiting on calculation and once calculation get completed “Done” message get printed on console.

await

Now same code with help of await can be done like this

public async void AwaitWithFactorial()
{
   for (int i = 1; i < 6; i++)
   {
       int temp = i;
       Task<KeyValuePair<int, string>> t = Task.Run(() => Factorial(temp));
       await t;
       Console.WriteLine(t.Result.Value);
      }
      Console.WriteLine("Done");
 
}

Above code is simple and clean there is no need of doing whole big refactoration which required when doing same thing with ContinueWith.

Summary

From above difference/comparison of ContinueWith vs await it clear that in many scenario using and await is very helpful.

But there is also scenario where more complex things not required with proper error/cancellation handling in that case continuewith is helpful. But this scenario is very rare.

It’s always good to go with simple, easy and clear solution, for this my suggestion is always go with await rather than ContinueWith.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here