等待异步函数从控制台Main()完成



如何在Main中等待RunScraper((函数在控制台应用程序中完成?

static void Main(string[] args)
{
RunScrapper();
}
static async void RunScrapper()
{
// Download the Chromium revision if it does not already exist
await new BrowserFetcher().DownloadAsync(BrowserFetcher.DefaultChromiumRevision);
// Create an instance of the browser and configure launch options
Browser browser = await Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = false
});
// Create a new page and go to Bing Maps
Page page = await browser.NewPageAsync();
await page.GoToAsync("https://www.google.es");
}

如果我放await RunScrapper();,它说Main((应该是异步的,RunScraper((应该返回一个Task。

问题是程序运行时什么也没发生,因为程序在RunScraper((生成任何东西之前就关闭了。

您可以将Main方法更改为Async。

public static async Task Main(string[] args)
{
await DoStuffAsync();
}
public static async Task DoStuffAsync(){
...    
}

相关内容

最新更新