Export (0) Print
Expand All

Task.FromResult<TResult> Method

.NET Framework 4.5

Creates a Task<TResult> that's completed successfully with the specified result.

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

public static Task<TResult> FromResult<TResult>(
	TResult result

Type Parameters


The type of the result returned by the task.


Type: TResult

The result to store into the completed task.

Return Value

Type: System.Threading.Tasks.Task<TResult>
The successfully completed task.

This method creates a Task<TResult> object whose Task<TResult>.Result property is result and whose Status property is TaskStatus.RanToCompletion. The method is commonly used when the return value of a task is immediately known without executing a longer code path. The example provides an illustration.

To create a Task object that does not return a value, retrieve the Task object from the CompletedTask property.

The following example is a command-line utility that calculates the number of bytes in the files in each directory whose name is passed as a command-line argument. Rather than executing a longer code path that instantiates a FileInfo object and retrieves the value of its FileInfo.Length property for each file in the directory, the example simply calls the FromResult<TResult> method to create a task whose Task<TResult>.Result property is zero (0).

using System;
using System.Collections.Generic;
using System.IO;
using System.Threading.Tasks;

public class Example
   public static void Main()
      string[] args = Environment.GetCommandLineArgs();
      if (args.Length > 1) {
         List<Task<long>> tasks = new List<Task<long>>();
         for (int ctr = 1; ctr < args.Length; ctr++)

         try {
         // Ignore exceptions here. 
         catch (AggregateException) {}

         for (int ctr = 0 ; ctr < tasks.Count; ctr++) {
            if (tasks[ctr].Status == TaskStatus.Faulted)
               Console.WriteLine("{0} does not exist", args[ctr + 1]);
               Console.WriteLine("{0:N0} bytes in files in '{1}'",
                                 tasks[ctr].Result, args[ctr + 1]);
      else {
         Console.WriteLine("Syntax error: Include one or more file paths.");

   private static Task<long> GetFileLengthsAsync(string filePath)
      if (! Directory.Exists(filePath)) {
         return Task.FromException<long>(
                     new DirectoryNotFoundException("Invalid directory name."));
      else {
         string[] files = Directory.GetFiles(filePath);
         if (filePath.Length == 0)
            return Task.FromResult(0L);
            return Task.Run( () => { long total = 0;
                                     foreach (var fileName in files) {
                                        FileInfo fi = new FileInfo(fileName);
                                        total = fi.Length;
                                     return total;
                                   } );
// When launched with the following command line arguments: 
//      subdir . newsubdir 
// the example displays output like the following: 
//       0 bytes in files in 'subdir' 
//       2,059 bytes in files in '.' 
//       newsubdir does not exist

.NET Framework

Supported in: 4.6, 4.5

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
© 2015 Microsoft