using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Threading;
using System.Threading.Channels;
using System.Threading.Tasks;
namespace ConsoleApp1
{
class Program
{
static void Main(string[] args)
{
//需要下载的url集合
string[] urls = new string[] {
"https://www.cnblogs.com",
"https://www.baidu.com",
"https://www.bing.com/"
};
//用于计时下载操作
Stopwatch stopwatch = new Stopwatch();
//开始计时下载url所需要的时间
stopwatch.Start();
var downloads = from url in urls
select CachedDownloads.DownloadStringAsync(url);
Task.WhenAll(downloads).ContinueWith(results =>
{
stopwatch.Stop();
//打印下载的字符数和运行时
C# Task.FromResult获取Task执行后的返回值或对象
最新推荐文章于 2024-05-29 16:11:20 发布