Export (0) Print
Expand All

TaskFactory.StartNew Method (Action)

Creates and starts a task.

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

public Task StartNew(
	Action action


Type: System.Action

The action delegate to execute asynchronously.

Return Value

Type: System.Threading.Tasks.Task
The started task.


The action argument is null.

Calling StartNew is functionally equivalent to creating a task by using one of its constructors, and then calling the Task.Start method to schedule the task for execution. Starting with the .NET Framework 4.5, you can use the Run method with an Action object as a quick way to call StartNew with default parameters. For more information and code examples, see Task.Run vs Task.Factory.StartNew in the Parallel Programming with .NET blog.

The following example uses the StartNew(Action) method to repeatedly invoke an Action delegate that generates a random number, interprets it as a Unicode code point, converts it to a UTF16-encoded code unit, and displays information about the resulting character or characters.

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

public class Example
   public static void Main()
      Random rnd = new Random();
      List<Task> tasks  = new List<Task>();
      // Execute the task 10 times. 
      for (int ctr = 1; ctr <= 9; ctr++) {
         tasks.Add(Task.Factory.StartNew( () => {
                                            int utf32 = 0;
                                            lock(rnd) {
                                               // Get UTF32 value.
                                               utf32 = rnd.Next(0, 0xE01F0);
                                            // Convert it to a UTF16-encoded character. 
                                            string utf16 = Char.ConvertFromUtf32(utf32);
                                            // Display information about the character.
                                            Console.WriteLine("0x{0:X8} --> '{1,2}' ({2})", 
                                                              utf32, utf16, ShowHex(utf16));

   private static string ShowHex(string value)
      string hexString = null;
      // Handle only non-control characters. 
      if (! Char.IsControl(value, 0)) {
         foreach (var ch in value)
            hexString += String.Format("0x{0} ", Convert.ToUInt16(ch));

      return hexString.Trim();
// The example displays the following output: 
//       0x00097103 --> '����' (0x55836 0x56579) 
//       0x000A98A1 --> '����' (0x55910 0x56481) 
//       0x00050002 --> '����' (0x55552 0x56322) 
//       0x0000FEF1 --> ' ﻱ' (0x65265) 
//       0x0008BC0A --> '����' (0x55791 0x56330) 
//       0x000860EA --> '����' (0x55768 0x56554) 
//       0x0009AC5A --> '����' (0x55851 0x56410) 
//       0x00053320 --> '����' (0x55564 0x57120) 
//       0x000874EF --> '����' (0x55773 0x56559)

.NET Framework

Supported in: 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

.NET for Windows Phone apps

Supported in: Windows Phone 8.1, Windows Phone Silverlight 8.1, 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