Share via


task::task Constructor

Constructs a task object.

task();

template<
   typename _Ty
>
explicit task(
   _Ty_Param
);

template<
   typename _Ty
>
explicit task(
   _Ty_Param,
   cancellation_token _Token
);

task(
   const task& _Other
);

task(
   task&& _Other
);

Parameters

  • _Ty
    The type of the parameter from which the task is to be constructed.

  • _Param
    The parameter from which the task is to be constructed. This could be a lambda, a function object, a task_completion_event<result_type> object, or a Windows::Foundation::IAsyncInfo if you are using tasks in your Metro style app. The lambda or function object should be a type equivalent to std::function<X(void)>, where X can be a variable of type result_type, task<result_type>, or a Windows::Foundation::IAsyncInfo in Metro style apps.

  • _Token
    The cancellation token to associate with this task. A task created without a cancellation token cannot be canceled. It implicitly receives the token cancellation_token::none().

  • _Other
    The source task object.

  • _CancellationToken

Remarks

The default constructor for a task is only present in order to allow tasks to be used within containers. A default constructed task cannot be used until you assign a valid task to it. Methods such as get, wait or then will throw an invalid_argument exception when called on a default constructed task.

A task that is created from a task_completion_event will complete (and have its continuations scheduled) when the task completion event is set.

The version of the constructor that takes a cancellation token creates a task that can be canceled using the cancellation_token_source the token was obtained from. Tasks created without a cancellation token are not cancelable.

Tasks created from a Windows::Foundation::IAsyncInfo interface or a lambda that returns an IAsyncInfo interface reach their terminal state when the enclosed Windows Runtime asynchronous operation or action completes. Similarly, tasks created from a lamda that returns a task<result_type> reach their terminal state when the inner task reaches its terminal state, and not when the lamda returns.

task behaves like a smart pointer and is safe to pass around by value. It can be accessed by multiple threads without the need for locks.

The constructor overloads that take a Windows::Foundation::IAsyncInfo interface or a lambda returning such an interface, are only available to Metro style apps.

For more information, see Task Parallelism (Concurrency Runtime).

Requirements

Header: ppltasks.h

Namespace: concurrency

See Also

Reference

task Class (Concurrency Runtime)