Sicos1977 / ChromiumHtmlToPdf

Convert HTML to PDF with a Chromium based browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

How to reuse Chrome

22140505 opened this issue · comments

commented

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

commented

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

commented

This solution should work, but when there are many threads, a lot of Chrome will be created, and it cannot be closed automatically.

commented

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.