BraisRV
BraisRV

Reputation: 31

C# headless chrome from Process Start not working

I need to call headless chrome from a net core console application. But with this code the aplication run and get stuck doing nothing and printing nothin, also the pdf is not created. The same arguments in the terminal are working as expected.

public static bool TakeScreenshot2()
        {
            try
            {
                var procStartInfo = new ProcessStartInfo()
                {
                    FileName = "google-chrome",
                    Arguments = "--headless --disable-gpu --print-to-pdf=final.pdf http://www.google.com/",
                    RedirectStandardOutput = true,
                    RedirectStandardError = true,
                    UseShellExecute = false,
                    CreateNoWindow = true
                };

                var proc = new Process { StartInfo = procStartInfo };
                proc.Start();

                var output = proc.StandardOutput.ReadToEnd();
                Console.WriteLine(output);
                string error = proc.StandardError.ReadToEnd();
                Console.WriteLine(error);

                return proc.ExitCode == decimal.Zero ? true : false;
            }
            finally
            {
                // do something
            }
        }

Upvotes: 1

Views: 966

Answers (1)

AnGG
AnGG

Reputation: 821

You should wait for the process to finish

var proc = new Process { StartInfo = procStartInfo };
proc.Start();
proc.WaitForExit();

You can check if it was success also with proc.ExitCode after it exit

If you dont want to block the thread unit it finish you can run it with, you function needs to be async

await Task.Run(() => proc.WaitForExit());

or to use the Process event Exited

Upvotes: 1

Related Questions