【问题标题】:C# Process: Using Pipes/File DescriptorsC# 进程:使用管道/文件描述符
【发布时间】:2018-11-29 00:11:08
【问题描述】:

我正在尝试连接到 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的要点

【问题讨论】:

    标签: c# process pipe


    【解决方案1】:

    答案在于以具有RedirectStandardInputRedirectStandardOutput 和/或RedirectStandardError 属性集的ProcessStartInfo 开头Process,然后使用Process.StandardInputProcess.StandardOutput 和/或@987654328 @ 属性来访问管道。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-03-28
      • 2021-05-23
      • 1970-01-01
      • 1970-01-01
      • 2018-12-02
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多