How to reuse Chrome
22140505 opened this issue · comments
I use a service for PDF conversion, which simply creates a static converter variable. Then, when the request is received, converttopdf() is called for conversion. However, I found the following anomalies occasionally:
An unhandled exception has occurred while executing the request.
System.NullReferenceException: Object reference not set to an instance of an object.
at ChromeHtmlToPdfLib.Browser.PrintToPdf(PageSettings pageSettings, CountdownTimer countdownTimer)
at ChromeHtmlToPdfLib.Converter.ConvertToPdf(ConvertUri inputUri, Stream outputStream, PageSettings pageSettings, String waitForWindowStatus, Int32 waitForWindowsStatusTimeout, Nullable`1 conversionTimeout, Nullable`1 mediaLoadTimeout, Stream logStream)
at ChromeHtmlToPdfLib.Converter.ConvertToPdf(ConvertUri inputUri, String outputFile, PageSettings pageSettings, String waitForWindowStatus, Int32 waitForWindowsStatusTimeout, Nullable`1 conversionTimeout, Nullable`1 mediaLoadTimeout, Stream logStream)
I can't find the reason.
Is it because of multithreading, or does chrome hibernate itself after it starts?
Please help me. Thank you
Are you using your code multithreaded? If so then you need to add the attribute [ThreadStatic]
to your static variable like this
[ThreadStatic] SomeVariableType TheVariable
This solution should work, but when there are many threads, a lot of Chrome will be created, and it cannot be closed automatically.
It should not be a problem to start chrome 10 or 20 times. I have a production machine that does something similar without any problems.