C#流程:使用管道/文件描述符

时间:2018-06-19 23:57:17

标签: c# process pipe

我正在尝试连接到Chromium in the same way Puppeteer does in NodeJS

这看起来是super simple in NodeJS。您向stdio数组添加了另外两个参数,并且有了管道。

我无法在Puppeteer-Sharp中实现相同的逻辑。我花了一些时间在这里阅读许多问题和答案。我读到有关AnonymousPipeServerStream的信息,但并不高兴。

这是一个我无法使其工作的示例:

AnonymousPipeServerStream streamReader = new AnonymousPipeServerStream(PipeDirection.In, HandleInheritability.Inheritable);
AnonymousPipeServerStream streamWriter = new AnonymousPipeServerStream(PipeDirection.Out, HandleInheritability.Inheritable);

var chromeProcess = new Process();
chromeProcess.EnableRaisingEvents = true;
chromeProcess.StartInfo.UseShellExecute = false;
chromeProcess.StartInfo.FileName = "/.local-chromium/MacOS-536395/chrome-mac/Chromium.app/Contents/MacOS/Chromium";
chromeProcess.StartInfo.Arguments = 
    "--MANY-MANY-ARGUMENTS  " +
    "--remote-debugging-pipe  " +
    "--user-data-dir=/var/folders/0k/4qzqprl541b74ddz4wwj_ph40000gn/T/mz0trgjc.vlj " +
    "--no-sandbox " +
    "--disable-dev-shm-usage " + 
    streamReader.GetClientHandleAsString() +
    streamWriter.GetClientHandleAsString();

chromeProcess.Start();

streamReader.DisposeLocalCopyOfClientHandle();
streamWriter.DisposeLocalCopyOfClientHandle();

Task task = Task.Factory.StartNew(async () =>
{
    var reader = new StreamReader(streamReader);
    while (true)
    {
        var response = await reader.ReadToEndAsync();

        if (!string.IsNullOrEmpty(response))
        {
            Console.WriteLine(response);
        }
    }
});

Console.ReadLine();

许多示例表明,您必须将GetClientHandleAsString()作为参数传递,但是我看不到它如何连接到进程。

这是the full example的要点

1 个答案:

答案 0 :(得分:0)

答案在于以Process开头的ProcessStartInfo设置了RedirectStandardInputRedirectStandardOutput和/或RedirectStandardError属性,然后使用{{ 1}},Process.StandardInput和/或Process.StandardOutput属性来访问管道。