Task<TResult>.Result Property

Gets the result value of this Task<TResult>.

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

[DebuggerBrowsableAttribute(DebuggerBrowsableState.Never)]
public TResult Result { get; }

Property Value

Type: TResult
The result value of this Task<TResult>, which is the same type as the task's type parameter.

ExceptionCondition
AggregateException

The task was canceled. The AggregateException.InnerExceptions collection contains a TaskCanceledException object.

-or-

An exception was thrown during the execution of the task. The AggregateException.InnerExceptions collection contains information about the exception or exceptions.

Accessing the property's get accessor blocks the calling thread until the asynchronous operation is complete; it is equivalent to calling the Wait method.

Once the result of an operation is available, it is stored and is returned immediately on subsequent calls to the Result property. Note that, if an exception occurred during the operation of the task, or if the task has been cancelled, the Result property does not return a value. Instead, attempting to access the property value throws an AggregateException exception.

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. If the directory contains files, it executes a lambda expression that instantiates a FileInfo object for each file in the directory and retrieves the value of its FileInfo.Length property. If a directory contains no files, it simply calls the FromResult<TResult> method to create a task whose Task<TResult>.Result property is zero (0). When the tasks finish, the total number of bytes in all a directory's files is available from the Result property.

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++)
            tasks.Add(GetFileLengthsAsync(args[ctr]));

         try {
            Task.WaitAll(tasks.ToArray());
         }
         // 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]);
            else
               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);
         else 
            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, 4

.NET Framework Client Profile

Supported in: 4

Portable Class Library

Supported in: Portable Class Library

Supported in: Windows Phone 8.1

Supported in: Windows Phone Silverlight 8.1

Supported in: Windows Phone Silverlight 8
Was this page helpful?
(1500 characters remaining)
Thank you for your feedback
Show:
© 2015 Microsoft