Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Task Constructor (Action^)

 

Initializes a new Task with the specified action.

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

public:
Task(
	Action^ action
)

Parameters

action
Type: System::Action^

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

Exception Condition
ArgumentNullException

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 Task::Run(Action^) or TaskFactory::StartNew(Action^) method. The only advantage offered by this constructor is that it allows object instantiation to be separated from task invocation.

The following example uses the Task(Action^) constructor to create tasks that retrieve the filenames in specified directories. All tasks write the file names to a single ConcurrentBag<T> object. The example then calls the WaitAll(array<Task^>^) method to ensure that all tasks have completed, and then displays a count of the total number of file names written to the ConcurrentBag<T> object.

No code example is currently available or this language may not be supported.

The following example is identical, except that it used the Run(Action^) method to instantiate and run the task in a single operation. The method returns the Task object that represents the task.

No code example is currently available or this language may not be supported.

Universal Windows Platform
Available since 8
.NET Framework
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Silverlight
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
Return to top
Show: