.NET应用程序如何重定向自己的标准输入流

本文关键字:自己的 标准输入流 重定向 应用程序 NET | 更新日期: 2023-09-27 18:25:25

我正在尝试仅使用标准输入流在2个C#应用程序之间进行双向通信(IPC)。父应用程序启动具有Process的子应用程序,其中RedirectStandardInput = true。因此,父进程能够使用childProc.StandardInput.WriteLine()向子进程发送命令。我使用Console.OpenStandardInput()获取的流的BeginRead()EndRead()异步捕获这些消息。亲子沟通非常有效。我能够异步发送和接收消息。

但是,当孩子尝试使用相同的代码向父级写入时,.NET会抛出以下错误:

StandardInput尚未重定向。

那么,简单地说,.NET应用程序如何重定向自己的标准输入流然后我将重定向父进程的标准输入流,以便子进程可以向其发送消息。

我的父进程是使用.NET 4.0 x86构建的WinForms C#应用程序。


编辑:这是IPC服务器的代码

internal class IPCServer {
    private Stream cmdStream;
    private byte[] cmdBuffer = new byte[4096];
    public IPCServer() {
        cmdStream = Console.OpenStandardInput(4096);
        GetNextCmd();
    }
    private void GetNextCmd() {
        // wait for next
        cmdStream.BeginRead(cmdBuffer, 0, cmdBuffer.Length, CmdRecived, null);
    }
    private void CmdRecived(IAsyncResult ar) {
        // input read asynchronously completed
        int bytesRead = 0;
        try {
            bytesRead = cmdStream.EndRead(ar);
        } catch (Exception) { }
        if (bytesRead > 0) {
            // accept cmd
            .........
            // wait for next
            GetNextCmd();
        }
    }

}

这是IPC客户端的代码

internal class IPCClient {
    private Process appProc;
    public IPCClient(Process appProcess) {
        if (!appProcess.StartInfo.RedirectStandardInput) {
            //MessageBox.Show("IPCClient : StandardInput for process '" + appProcess.ProcessName + "' has not been 'redirected'!");
            return;
        }
        appProc = appProcess;
    }
    public void SendCmd(string cmd) {
        if (appProc != null) {
            appProc.StandardInput.WriteLine(cmd);
        }
    }
}

在父进程中:

// open child app
ProcessStartInfo info = new ProcessStartInfo(childProcPath, args);
info.WorkingDirectory = childProcDir;
info.LoadUserProfile = true;
info.UseShellExecute = false;
info.RedirectStandardError = true;
info.RedirectStandardOutput = true;
info.RedirectStandardInput = true;
childProc = Process.Start(info);
// connect to app for IPC
Client = new IPCClient();
Client.Init(childProc);
// recieve cmds from app
Server = new IPCServer();
Server.OnCmdRecieved = GotCmd;
Server.Init();

在子进程中:

ownerProcess = ....
Server = new IPCServer();
Server.OnCmdRecieved = GotCmd;
Server.Init();
Client = new IPCClient();
Client.Init(ownerProcess);

.NET应用程序如何重定向自己的标准输入流

您可以用一种更方便的方式来完成。只需让您的客户端应用程序对输入''输出重定向一无所知。服务器应该为重定向而烦恼。

我已经使用您的技术实现了一个最简单的客户端-服务器应用程序,它演示了应该如何使用它。

客户

客户端是一个最简单的应用程序,它只需回显消息并在您输入exit:时关闭

static void Main(string[] args)
{
    string str;
    do
    {
        str = Console.ReadLine();
        Console.WriteLine("Executed: " + str);
    } while (str != "exit");
}

客户端对重定向一无所知,它只是像控制台应用程序一样工作,并在控制台中读写。

服务器

服务器运行客户端进程并在其流中读写。

Process p = new Process();
p.StartInfo.FileName = "ConsoleApplication1.exe";
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardInput = true;
p.StartInfo.RedirectStandardOutput = true;
p.Start();
p.StandardInput.WriteLine("a");
p.StandardInput.WriteLine("b");
p.StandardInput.WriteLine("c");
p.StandardInput.WriteLine("d");
p.StandardInput.WriteLine("exit");
p.StandardInput.WriteLine("f");
p.StandardInput.Close();
string res = p.StandardOutput.ReadToEnd();

执行此代码后,res包含以下文本:

Executed: a
Executed: b
Executed: c
Executed: d
Executed: exit

如果子进程只是在子进程的StandardOutput流上使用BeginRead()EndRead()会怎样。这应该行得通!