Task::Run<TResult> Method (Func<TResult>^)
Queues the specified work to run on the thread pool and returns a Task<TResult> object that represents that work.
Assembly: mscorlib (in mscorlib.dll)
Parameters
- function
-
Type:
System::Func<TResult>^
The work to execute asynchronously.
Return Value
Type: System.Threading.Tasks::Task<TResult>^A task object that represents the work queued to execute in the thread pool.
Type Parameters
- TResult
The return type of the task.
| Exception | Condition |
|---|---|
| ArgumentNullException | The function parameter is null. |
The Run<TResult> method is a simpler alternative to the TaskFactory::StartNew(Action^) method. It creates a task with the following default values:
Its cancellation token is CancellationToken::None.
Its CreationOptions property value is TaskCreationOptions::DenyChildAttach.
It uses the default task scheduler.
For information on handling exceptions thrown by task operations, see Exception Handling (Task Parallel Library).
The following example counts the approximate number of words in text files that represent published books. Each task is responsible for opening a file, reading its entire contents asynchronously, and calculating the word count by using a regular expression. The WaitAll(array<Task^>^) method is called to ensure that all tasks have completed before displaying the word count of each book to the console.
Available since 8
.NET Framework
Available since 4.5
Portable Class Library
Supported in: portable .NET platforms
Windows Phone Silverlight
Available since 8.0
Windows Phone
Available since 8.1
The regular expression \p{P}*\s+ matches zero, one, or more punctuation characters followed by one or more whitespace characters. It assumes that the total number of matches equals the approximate word count.