C# 进程 : Using Pipes/File Descriptors

标签 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 的要点

最佳答案

答案在于启动 ProcessProcessStartInfoRedirectStandardInput , RedirectStandardOutput和/或 RedirectStandardError属性设置,然后使用 Process.StandardInput , Process.StandardOutput和/或 Process.StandardError访问管道的属性。

关于C# 进程 : Using Pipes/File Descriptors,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50938404/

相关文章:

java - 如何通过Java程序在windows中查看Tomcat服务是否正在运行或停止?

windows - 在进程完成之前,Windows cmd.exe 中的管道不会转发标准输出?

bash - 在管道之后,命令被认为以空格为前缀

c# - List.Contains 无法正常工作

c# - 处理 400 万条记录时内存不足

c# - MVC4 TDD - System.ArgumentNullException : Value cannot be null.

C# TCPClient 如何让错误地址/离线服务器更快失败

linux - 我应该如何在后台运行我的 Golang 进程?

c# - 如何从 C# 代码运行 R 脚本?

linux - 在创建管道、fifo 或套接字时创建 inode