簡體   English   中英

來自 Process Start 的 C# 無頭鍍鉻不起作用

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

我需要從網絡核心控制台應用程序調用無頭 chrome。 但是使用此代碼,應用程序運行並卡在無所事事且無打印,pdf 也沒有創建。 終端中相同的 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
            }
        }

您應該等待該過程完成

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

您可以在退出后使用proc.ExitCode檢查它是否成功

如果你不想阻塞它完成的線程單元,你可以運行它,你 function 需要異步

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

或使用 Process 事件Exited

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM