I am using puppeteersharp for some scraping. I want to manipulate the Page within a different thread, for example to get the html of the page in periodic intervals (the logic is not important).
Every time I try to call puppeteer inside a Thread the execution is being stack in that line.
In this example code:
Browser _puppeteerBrowser = await PuppeteerSharp.Puppeteer.LaunchAsync(new LaunchOptions
{
Headless = false,
ExecutablePath = _chromePath
});
Page Page = (await _puppeteerBrowser.PagesAsync()).FirstOrDefault();
var task = Task.Run(async () =>
{
var content = await Page.GetContentAsync(); // it never returns!!
System.Console.WriteLine(content.Length);
});
await Page.GoBackAsync(); // it works fine
task.Wait(); // never ends because of the stack inside the thread
I tried different variations with Timer, Task, Thread but every time I tried to do something in puppeteer inside another thread it hangs. How can I fix this? And in general if we want to use the same Puppeteer in different thread (for example maybe we want to observe if something is changed in the browser outside of the 'main flow') how can this be done?