Starting a new task automatically when another task completes

1. How to start a new task automatically when another task completes?

To start a new task automatically when another task completes we can use a ContinueWith method.

static int GetResult(int param)

{

    Trace.WriteLine(“Thread Id:” + Thread.CurrentThread.ManagedThreadId.ToString());

    return param;

}

static void ContinueWithTest()

{

    Task<int> task = new Task<int>(n => GetResult((int)n), 10000000);

    task.Start();

    Task cwt = task.ContinueWith(

        completedTask =>

        {

            Trace.WriteLine(“Thread Id: ” + Thread.CurrentThread.ManagedThreadId.ToString());

            Trace.WriteLine(“The result is:” + String.Format(“{0} and status is {1}”

                , completedTask.Result

                , completedTask.Status));

            }

        , TaskContinuationOptions.OnlyOnRanToCompletion);

}

We can specify the behavior for a task that is created by using the ContinueWith method.

Member name Description
None Default = “Continue on any, no task options, run asynchronously” Specifies that the default behavior should be used. Continuations, by default, will be scheduled when the antecedent task completes, regardless of the task’s final TaskStatus.
PreferFairness A hint to a TaskScheduler to schedule a task in as fair a manner as possible, meaning that tasks scheduled sooner will be more likely to be run sooner, and tasks scheduled later will be more likely to be run later.
LongRunning Specifies that a task will be a long-running, course-grained operation. It provides a hint to the TaskScheduler that oversubscription may be warranted.
AttachedToParent Specifies that a task is attached to a parent in the task hierarchy.
NotOnRanToCompletion Specifies that the continuation task should not be scheduled if its antecedent ran to completion. This option is not valid for multi-task continuations.
NotOnFaulted Specifies that the continuation task should not be scheduled if its antecedent threw an unhandled exception. This option is not valid for multi-task continuations.
NotOnCanceled Specifies that the continuation task should not be scheduled if its antecedent was canceled. This option is not valid for multi-task continuations.
OnlyOnRanToCompletion Specifies that the continuation task should be scheduled only if its antecedent ran to completion. This option is not valid for multi-task continuations.
OnlyOnFaulted Specifies that the continuation task should be scheduled only if its antecedent threw an unhandled exception. This option is not valid for multi-task continuations.
OnlyOnCanceled Specifies that the continuation task should be scheduled only if its antecedent was canceled. This option is not valid for multi-task continuations.
ExecuteSynchronously Specifies that the continuation task should be executed synchronously. With this option specified, the continuation will be run on the same thread that causes the antecedent task to transition into its final state. If the antecedent is already complete when the continuation is created, the continuation will run on the thread creating the continuation. Only very short-running continuations should be executed synchronously.