简体   繁体   English

来自 Process Start 的 C# 无头镀铬不起作用

[英]C# headless chrome from Process Start not working

I need to call headless chrome from a net core console application.我需要从网络核心控制台应用程序调用无头 chrome。 But with this code the aplication run and get stuck doing nothing and printing nothin, also the pdf is not created.但是使用此代码,应用程序运行并卡在无所事事且无打印,pdf 也没有创建。 The same arguments in the terminal are working as expected.终端中相同的 arguments 按预期工作。

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您可以在退出后使用proc.ExitCode检查它是否成功

If you dont want to block the thread unit it finish you can run it with, you function needs to be async如果你不想阻塞它完成的线程单元,你可以运行它,你 function 需要异步

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

or to use the Process event Exited或使用 Process 事件Exited

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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