Sicos1977 / ChromiumHtmlToPdf

Convert HTML to PDF with a Chromium based browser

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Hanging at Enabling/Disabling caching

pamar1908 opened this issue · comments

Reopening an older discussion I still face the same issue with the converter hanging forever at the Enabling or Disabling cashing step. I wanted to add the following:

  • It happens with both Chrome and Edge (expected I would say, though it means it is not because of some corrupt userprofile data)
  • It happens even with userprofile directory set to Downloads
  • The issue occurs with .Net Framework 4.8
  • The issue does not occur with .Net 6.0

Is there something weird being done with .Net Framework 4.8? Can we find a fix for it?

commented

I also use this library with .net 4.8 and don't have any problems with enabling or disabling the cache.

At this moment I'm busy with rewriting the code to make everything async awaitable. Don't know if it will make a difference but it is probably better than having code that is a mix between normal and async code.

commented

I just published a new version to nuget. This code is all async. Please try this one and see if it fixes you issue

Thank you very much. This has resolved the above issue. It completes the pdf conversion and file creation, but hangs at the "Closing Microsoft Edge browser gracefully" step which corresponds to the END USING the converter code line. On another note, when Chrome was used it threw the "Google Chrome exited unexpectedly" exception as mentioned by other people since it is already running.

commented

This should be fixed in the 4.0.1 version. I had the same problem. It kept hanging on the web socket close code.

commented

It was this part that caused a deadlock issue --> 3034e56

I am already on the 4.0.1 version. Regarding Chrome already running is there a workaround?

commented

Where exactly does it hang because my problem was solved when fixing that Close web socket issue.

So I now have no idea where it gives a problem for you.

commented

Rewriting code to async code is always a little bit tricky because async code works a different way than synchronous code.

Now Edge was closed gracefully so maybe there was some hanging process from before. Chrome still quits unexpectedly. Is there a way to run when chrome is already running by user?

commented

When testing yesterday I had simular behaviour. I don't know how you designed your code but I'm using my library from a Windows Services and when starting this service I always check for already running Chrome or Edge instances and kill them to prevent this kind of weird behaviour

commented
    /// <summary>
    ///     This will kill all running Edge processes
    /// </summary>
    /// <returns>
    ///     Returns the output of this function
    /// </returns>
    // ReSharper disable once UnusedMethodReturnValue.Local
    private string KillAllEdgeProcesses()
    {
        var result = new StringBuilder();
        result.AppendLine("=== START KILLING Edge PROCESSES ===");

        foreach (var process in Process.GetProcessesByName("msedge"))
            try
            {
                result.AppendLine($"Killing Edge process with id {process.Id}");
                process.Kill();
                result.AppendLine($"Killed Edge process with id {process.Id}");
            }
            catch (Exception exception)
            {
                result.AppendLine($"Could not kill Edge process with id {process.Id}, error {exception.Message}");
            }

        result.AppendLine("=== END KILLING Edge PROCESSES ===");
        return result.ToString();
    }
    #endregion

Thank you for the snippet. Is there a way to run without killing processes running by user? Puppeteer for example is not failing when chrome is running. I read some comments by others here that the old headless mode does the trick as it was completely independent of the headed mode

commented

You can try to use the old headless mode. I added a property so that you can switch back to it.

I think I called is UseOldHeadlessMode