I'm trying to call Browser.NewPageAsync()
in another static method, but when I call it, the method in which it was called just exits.
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();
}
}
For example: If I call Browser.NewPageAsync()
in MainAsync()
, then Browser.NewPageAsync()
will be called as expected.