我正试图把我的头绕在async await的C#上。我已经写了这个小窗口控制台应用程序,有两个文件。
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();
}
}
}我只想从我的DownloadFilesAsync()调用函数main。我已经创建了Downloader对象'd‘。但是,由于它是main类型,并且返回类型必须是无效的,所以不可能。有什么办法可以绕过这件事?
发布于 2017-10-11 02:10:43
Task.Run(async () => { await d.DownloadFilesAsync();}).Wait();https://stackoverflow.com/questions/46678636
复制相似问题