我试图调用Browser.NewPageAsync()
另一个静态方法,但是当我调用它时,调用它的方法就退出了。
partial class Program
{
static Browser Browser;
static async Task StartBrowser()
{
Browser = await Puppeteer.LaunchAsync
(
new LaunchOptions
{
Headless = true,
ExecutablePath = "Chromium\\chrome.exe"
}
);
Console.WriteLine("Browser launched");
}
static void StartScraping(int threads)
{
for (int i = 0; i < threads; i++)
{
Task.Run(async () =>
{
int ThreadNumber = i;
Console.WriteLine("Thread #" + ThreadNumber + " started");
Page p = await Browser.NewPageAsync(); //exits here
await p.GoToAsync("https://www.google.com");
Console.WriteLine("Content:\n" + await p.GetContentAsync());
});
}
}
static async Task MainAsync()
{
await StartBrowser();
StartScraping(1);
}
static void Main(string[] args)
{
MainAsync().GetAwaiter().GetResult();
}
}
例如:如果我调用Browser.NewPageAsync()
,MainAsync()
thenBrowser.NewPageAsync()
将按预期调用。