Task Constructor (Action^)
Initializes a new Task with the specified action.
Assembly: mscorlib (in mscorlib.dll)
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.
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.
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