本文整理汇总了C#中System.Threading.Tasks.Task<TResult>.Result属性的典型用法代码示例。如果您正苦于以下问题:C# Task<TResult>.Result属性的具体用法?C# Task<TResult>.Result怎么用?C# Task<TResult>.Result使用的例子?那么恭喜您, 这里精选的属性代码示例或许可以为您提供帮助。您也可以进一步了解该属性所在类System.Threading.Tasks.Task<TResult>
的用法示例。
在下文中一共展示了Task<TResult>.Result属性的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.Collections.Generic;
using System.IO;
using System.Threading;
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 (files.Length == 0)
return Task.FromResult(0L);
else
return Task.Run( () => { long total = 0;
Parallel.ForEach(files, (fileName) => {
var fs = new FileStream(fileName, FileMode.Open,
FileAccess.Read, FileShare.ReadWrite,
256, true);
long length = fs.Length;
Interlocked.Add(ref total, length);
fs.Close(); } );
return total;
} );
}
}
}
// When launched with the following command line arguments:
// subdir . newsubdir
输出:
0 bytes in files in 'subdir' 2,059 bytes in files in '.' newsubdir does not exist