Task<'TResult> Constructor (Func<'TResult>)
Initializes a new Task<'TResult> with the specified function.
Assembly: mscorlib (in mscorlib.dll)
Rather than calling this constructor, the most common way to instantiate a Task<'TResult> object and launch a task is by calling the static Task.Run<'TResult>(Func<'TResult>) and TaskFactory<'TResult>.StartNew(Func<'TResult>) methods. The only advantage offered by this constructor is that it allows object instantiation to be separated from task invocation.
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 Task.WaitAll(Task) method is called to ensure that all tasks have completed before displaying the word count of each book to the console.
Object instantiation is separated from object execution in this example so that the example can ensure that each file exists. If they do not, it displays the name of the missing file. Otherwise, it calls the Task.Start method to launch each task.
Available since 8
Available since 4.0
Portable Class Library
Supported in: portable .NET platforms
Available since 5.0
Windows Phone Silverlight
Available since 8.0
Available since 8.1