Task Constructor (Action<Object>, Object)

Initializes a new Task with the specified action and state.

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

public Task(
	Action<Object> action,
	Object state


Type: System.Action<Object>

The delegate that represents the code to execute in the task.

Type: System.Object

An object representing data to be used by the action.


The action argument is null.

Rather than calling this constructor, the most common way to instantiate a Task object and launch a task is by calling the static TaskFactory.StartNew(Action<Object>, Object) method. The only advantage offered by this constructor is that it allows object instantiation to be separated from task invocation.

The following example defines an array of 6-letter words. Each word is then passed as an argument to the Task(Action<Object>, Object) constructor, whose Action<T> delegate scrambles the characters in the word, then displays the original word and its scrambled version.

using System;
using System.Collections.Generic;
using System.Threading.Tasks;

public class Example
   public static void Main()
      var tasks = new List<Task>();
      Random rnd = new Random();
      Object lockObj = new Object();
      String[] words6 = { "reason", "editor", "rioter", "rental",
                          "senior", "regain", "ordain", "rained" };

      foreach (var word6 in words6) {
         Task t = new Task( (word) => { Char[] chars = word.ToString().ToCharArray();
                                        double[] order = new double[chars.Length];
                                        lock (lockObj) {
                                           for (int ctr = 0; ctr < order.Length; ctr++)
                                              order[ctr] = rnd.NextDouble();
                                        Array.Sort(order, chars);
                                        Console.WriteLine("{0} --> {1}", word,
                                                          new String(chars));
                                      }, word6);
// The example displays output like the following: 
//    regain --> irnaeg 
//    ordain --> rioadn 
//    reason --> soearn 
//    rained --> rinade 
//    rioter --> itrore 
//    senior --> norise 
//    rental --> atnerl 
//    editor --> oteird

.NET Framework

Supported in: 4.6, 4.5, 4

.NET Framework Client Profile

Supported in: 4

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, Windows Phone Silverlight 8

Portable Class Library

Supported in: Portable Class Library
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
© 2015 Microsoft