C#处理:使用管道/文件描述符

问题描述 投票:5回答:1

我正在尝试连接到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个回答
0
投票

答案在于用Process启动ProcessStartInfoRedirectStandardInput具有RedirectStandardOutputRedirectStandardError和/或Process.StandardInput属性集,然后使用Process.StandardOutputProcess.StandardError和/或qazxswpoi属性来访问管道。

© www.soinside.com 2019 - 2024. All rights reserved.