【发布时间】:2017-10-11 10:10:43
【问题描述】:
我正在尝试用 C# 围绕 async await 转转。我编写了这个有两个文件的小型 Windows 控制台应用程序。
Downloader.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace AsyncAwait
{
public class Downloader
{
public async Task DownloadFilesAsync()
{
// In the Real World, we would actually do something...
// For this example, we're just going to print file 0, file 1.
await DownloadFile0();
await DownloadFile1();
}
public async Task DownloadFile0()
{
Console.WriteLine("Downloading File 0");
await Task.Delay(100);
}
public async Task DownloadFile1()
{
Console.WriteLine("Downloading File 1");
await Task.Delay(100);
}
}
}
Program.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace AsyncAwait
{
class Program
{
static void Main(string[] args)
{
Downloader d = new Downloader();
}
}
}
我只想从我的main 调用函数DownloadFilesAsync()。我创建了Downloader 对象'd'。但是,由于它是 main 并且返回类型必须为 void,因此这是不可能的。有什么办法解决这个问题?
【问题讨论】:
标签: c# async-await