简体   繁体   中英

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
            }
        }

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM