Export (0) Print
Expand All
Expand Minimize

TaskFactory.ContinueWhenAny Method (Task[], Action<Task>)

Creates a continuation Task that will be started upon the completion of any Task in the provided set.

Namespace:  System.Threading.Tasks
Assemblies:   mscorlib (in mscorlib.dll)
  System.Threading.Tasks (in System.Threading.Tasks.dll)

public Task ContinueWhenAny(
	Task[] tasks,
	Action<Task> continuationAction


Type: System.Threading.Tasks.Task[]

The array of tasks from which to continue when one task completes.

Type: System.Action<Task>

The action delegate to execute when one task in the tasks array completes.

Return Value

Type: System.Threading.Tasks.Task
The new continuation Task.


The exception that is thrown when one of the elements in the tasks array has been disposed.


The exception that is thrown when the tasks array is null.


The exception that is thrown when the continuationAction argument is null.


The exception that is thrown when the tasks array contains a null value.


The exception that is thrown when the tasks array is empty.

The following example shows how to use ContinueWhenAny and ContinueWhenAll:

using System;
using System.Threading;
using System.Threading.Tasks;

class ContinueWhenMultiDemo
    // Demonstrated features: 
    // 		Task.Factory 
    //		TaskFactory.ContinueWhenAll() 
    //		TaskFactory.ContinueWhenAny() 
    //		Task.Wait() 
    // Expected results: 
    // 		Three tasks are created in parallel.  
    //		Each task for a different period of time prints a number and returns it. 
    //      A ContinueWhenAny() task indicates the first of the three tasks to complete. 
    //      A ContinueWhenAll() task sums up the results of the three tasks and prints out the total. 
    // Documentation: 
    //		http://msdn.microsoft.com/en-us/library/system.threading.tasks.taskfactory_members(VS.100).aspx 
    static void Main()
        // Schedule a list of tasks that return integer
        Task<int>[] tasks = new Task<int>[]
				Task<int>.Factory.StartNew(() => 
						Console.WriteLine("Task={0}, Thread={1}, x=5", Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
						return 5;

				Task<int>.Factory.StartNew(() => 
						Console.WriteLine("Task={0}, Thread={1}, x=3", Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
						return 3;

				Task<int>.Factory.StartNew(() => 
						Console.WriteLine("Task={0}, Thread={1}, x=2", Task.CurrentId, Thread.CurrentThread.ManagedThreadId);
						return 2;

        // Schedule a continuation to indicate the result of the first task to complete
        Task.Factory.ContinueWhenAny(tasks, winner =>
            // You would expect winning result = 3 on multi-core systems, because you expect 
            // tasks[1] to finish first.
            Console.WriteLine("Task={0}, Thread={1} (ContinueWhenAny): Winning result = {2}", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, winner.Result);

        // Schedule a continuation that sums up the results of all tasks, then wait on it. 
        // The list of antecendent tasks is passed as an argument by the runtime.
            (antecendents) =>
                int sum = 0;
                foreach (Task<int> task in antecendents)
                    sum += task.Result;

                Console.WriteLine("Task={0}, Thread={1}, (ContinueWhenAll): Total={2} (expected 10)", Task.CurrentId, Thread.CurrentThread.ManagedThreadId, sum);


.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

.NET for Windows Store apps

Supported in: Windows 8

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8

Windows Phone 8.1, Windows Phone 8, Windows 8.1, Windows Server 2012 R2, Windows 8, Windows Server 2012, Windows 7, Windows Vista SP2, Windows Server 2008 (Server Core Role not supported), Windows Server 2008 R2 (Server Core Role supported with SP1 or later; Itanium not supported)

The .NET Framework does not support all versions of every platform. For a list of the supported versions, see .NET Framework System Requirements.

© 2014 Microsoft