如何运行MSDN代码示例“如何:创建预计算任务”
本文关键字:如何 创建 任务 计算 何运行 运行 代码 MSDN | 更新日期: 2023-09-27 18:12:25
尝试在。net 4.0异步CTP中运行c#代码示例"How to: Create precomputed Tasks" (MSDN),已做了更改:
-
Task.FromResult
—>TaskEx.FromResult
-
Task.FromResult
—>TaskEx.FromResult
-
Task.Run(async () =>
->TaskEx.RunEx(async () =>
(也试过TaskEx.Run(async () =>
)
获取编译错误:
因为'
System.Func<System.Threading.Tasks.Task>
'是异步的并且返回任务中,return关键字不能后跟对象表达式
如何更改代码以运行此示例?
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Threading.Tasks;
// Demonstrates how to use Task<TResult>.FromResult to create a task
// that holds a pre-computed result.
class CachedDownloads
{
// Holds the results of download operations.
static ConcurrentDictionary<string, string> cachedDownloads =
new ConcurrentDictionary<string, string>();
// Asynchronously downloads the requested resource as a string.
public static Task<string> DownloadStringAsync(string address)
{
// First try to retrieve the content from cache.
string content;
if (cachedDownloads.TryGetValue(address, out content))
{
return TaskEx //****** Task.FromResult ---> TaskEx.FromResult
.FromResult<string>(content);
}
// If the result was not in the cache, download the
// string and add it to the cache.
return TaskEx.RunEx //****** Task.Run --> TaskEx.RunEx (TaskEx.Run)
(async () =>
{
content = await new WebClient().DownloadStringTaskAsync(address);
cachedDownloads.TryAdd(address, content);
return content;//*****Error
});
}
static void Main(string[] args)
{
// The URLs to download.
string[] urls = new string[]
{
"http://msdn.microsoft.com",
"http://www.contoso.com",
"http://www.microsoft.com"
};
// Used to time download operations.
Stopwatch stopwatch = new Stopwatch();
// Compute the time required to download the URLs.
stopwatch.Start();
var downloads = from url in urls
select DownloadStringAsync(url);
TaskEx.WhenAll(downloads)//*** Task.WhenAll --> TaskEx.WhenAll
.ContinueWith(results =>
{
stopwatch.Stop();
// Print the number of characters download and the elapsed time.
Console.WriteLine
("Retrieved {0} characters. Elapsed time was {1} ms."
, results.Result.Sum(result => result.Length)
, stopwatch.ElapsedMilliseconds);
})
.Wait();
// Perform the same operation a second time. The time required
// should be shorter because the results are held in the cache.
stopwatch.Restart();
downloads = from url in urls
select DownloadStringAsync(url);
TaskEx.WhenAll(downloads)//*** Task.WhenAll --> TaskEx.WhenAll
.ContinueWith(results =>
{
stopwatch.Stop();
// Print the number of characters download and the elapsed time.
Console.WriteLine("Retrieved {0} characters. Elapsed time was {1} ms.",
results.Result.Sum(result => result.Length),
stopwatch.ElapsedMilliseconds);
})
.Wait();
}
}
更新:
这个代码示例没有我的更新异步CTP运行在。net 4.5?c#编译器在VS2012中得到了增强,在async
lambdas(即Func<Task>
及其家族)存在的情况下,可以更智能地处理泛型重载解析。因为你是在VS2010上,所以你必须在这里和那里帮助它。
线索在您的错误消息中显示的async
lambda的类型:System.Func<System.Threading.Tasks.Task>
.
应该是System.Func<System.Threading.Tasks.Task<System.String>>
。
那么我就试试这个:
return TaskEx.RunEx<string> //****** Task.Run --> TaskEx.RunEx (TaskEx.Run)
(async () =>
{
content = await new WebClient().DownloadStringTaskAsync(address);
cachedDownloads.TryAdd(address, content);
return content;//*****Error
});